常用数据库查询语句

数据库常用查询语句(DQL)

基本查询

select 字段1, 字段2,…from 表名;

例如:select id , name from stu;

条件查询

select 字段1, 字段2,…from 表名 where 字段 关系符号 值 ;

关系符号

< = >= <= != 大于 等于 大于等于 小于等于 不等于

例如:select * from stu id > 2;

and or in(范围内满足in内部条件) not in 相反

例如: select * from stu where id>1 and age <40;

​ select * from stu where id >1 or name =‘张三’;

​ select * from stu where id in(3,4);

between 值1 and 值2 在[值1,值2]之间 包含两边临界值

例如: select * from stu where id between 2 and 4;

模糊查询

select * from 表名 where 字段 like ‘%值%’;

例如: select * from stu where name like ‘陈%’;

注: %的位置不同 表达的意思不同 陈% : 陈某某 ,%陈% : 某陈某,%陈:某某陈

% 匹配任意字符 (%可以是任意长度)_匹配指定长度字符 一个_代表一个长度

排序查询

select * from 表名 order by 字段 排序类型 asc 升序 desc 降序 没写排序类型 默认 升序

例: select * from stu order by id desc ;

聚合函数 多行数据一行返回

count(字段) 计数 计算该列不为空的数据个数

例 :select count(name) from stu;

sum(字段) 求和 计算该列所有数字的和 字符串求和结果为0

例:select sum(age) from stu;

max(字段) 最大值 获取该列最大值

例: select max(age) from stu;

min(字段) 最小值 获取该列最小值

例: select min(age) from stu;

avg(字段) 平均值 不为null的进行平均

例: select avg(age) from stu;

注:聚合函数要放在select 和 from 之间

去重

distinct(列) 一般配合count()一起使用

分组查询

select * from stu group by 字段

你可能感兴趣的:(mysql,dml)