zoukankan      html  css  js  c++  java
  • sql 基础查询集锦

    授权
    GRANT All ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION;
    
    解除授权
    REVOKE ALL ON *.* FROM 'root'@'%';
    
    刷新授权权限表
    FLUSH PRIVILEGES;
    
    删除数据库
    drop database 库名;
    
    
    修改数据库
    alter database 库名  属性 set 属性值;
    例:alter databse user character set utf8;
    
    
    查看表单属性
    desc 表名;
    查看建表语句
    show create table 表名; 重命名表 rename table 原表名 to 新表名; 删除列 alter table 表名 drop 列名; 删除行 delete FROM 表名 where 字段名='字段值'; 更改数据 update 表名 set 字段名='' where 条件 格式化删除 truncate truncate table 表名; 创建表 create table 表名( 字段名 字段属性, .... .... , ... .... 注意此处不可以有逗号 ); 查看数据库中表单 show tables; table后面有s; 数据插入 insert into 表名 (列名,列名……) values (值,值,……); 批量插入 insert into 表名 (列名,列名……) values (值,值,……), (值,值,……), (值,值,……); 查询数据(条件查询) 1.AND 2.OR 3.between and: select * from 表名 where 字段名 between '值A' and '值B'; A<B 4.in: selsct * from 表名 where 字段名 in(值A,值B,……); IN前可加NOT 5.!= = > < <>(不等于) <= >= 模糊查询 1.包含字符a :like '%a%'; 2.第3个是字符a:like '__a%'; 字段控制查询 1.去重 distinct:select distinct * from 表名; 2.求和:select 无空值的列+ifnull(允许为空的列,0) [as] sum from 表名; sum:等于给查询结果重命名 as可省略 3.排序:升序 order by 列A [asc],列B [asc],; 降序 order by 列A desc ,列B [desc] ; 列A相等时,以列B排列; 4.聚合函数 <1>.查询表中总记录条数 select count(*) as 总条数 FROM 表名; <2>.查询表中有一个或多个字段中为非空值的数量 select count(字段名),count(字段名)…… as 条数 from 表名; <3>.查询表中某一字段值满足条件的数量 select count(*) as 条数 from 表名 where 字段名>条件; select count(*) as 条数 from 表名 where 字段名+ifnull(字段名,0)>条件; <4>.求总和 select sum(字段名1+ifnull(字段名2,0)) as 字段1和字段2的全部数据的和 from 表名; select sum(字段名1)+sum(字段名2) as 字段1和字段2的全部数据的和 from 表名; <5>.求平均 select avg(字段名) as 字段的平均值 from 表名; <6>.查询最高值 select max(字段名) as 字段的最高值,min(字段名) as 字段名的最低值 from 表名; <7>.分组查询 select 外联表链接字段,函数(本表字段) from 表名 where 条件 group by 外联表链接字段; <8>.筛选having子句 【可以用在group by 后 而where不可在group by 后用】 select 外联表链接字段,函数(本表字段) from 表名 where 条件 group by 外联表链接字段 having (必须是此sesect后的字段)>条件;
    方言:limit
    查询10行记录,起始行从3开始
    SELECT * FROM emp LIMIT 3, 10;
    

                           关于模糊查询详细介绍:http://www.cnblogs.com/ssjifm/p/7349196.html

  • 相关阅读:
    LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
    LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
    LeetCode 617. 合并二叉树(Merge Two Binary Trees)
    LeetCode 206. 反转链表(Reverse Linked List) 16
    LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
    LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
    LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
  • 原文地址:https://www.cnblogs.com/ssjifm/p/7347852.html
Copyright © 2011-2022 走看看