表单查询
查询所有字段
select 字段名1,字段名2,.....from 表名;
顺序可以相反
select * from 表名;
查询表的所有数据
条件查询
select 字段1,字段2,...from 表名;
where 字段表达式;
select 字段1,字段2,...from 表名;
where 字段表达式 and(or) 字段表达式2;
select 字段1,字段2,。。。from 表名
where 字段名 [not] in (元素1,元素2,...);带关键字查询
select 字段1,字段2,.。。from 表名
where 字段名 is [not] null;空值查询
select 字段1,字段2.。。from 表名
where 字段名 [not] between 值1 and 值2;范围查询
select 字段1,字段2,.。。from 表名
where 字段名 [not] like ‘匹配字符串’;
可以包含%和_通配符字符串 _字段串表示任意字符 %表示任意0-n的字符
select distinct 字段名 from 表名;
去重查询
高级查询
排序查询
select 字段1,字段2,... from 表名
order by 字段1 [asc|desc],字段名2[asc|desc]。。。;
asc代表升序 desc 代表降序 默认升序
聚合函数
select count (*|1|列名) from 表名;
返回列的行数
select count(1) (as)别名 from 表名
count(1)没主键的情况下比count(*)效率高
IFNULL(字段1,0)字段1中出现NULL替换成0
select sum(字段名1),sum(字段名2) from 表名;
select sum(字段名1+IFNULL(字段名2,0)) as 别名 from 表名;
select avg(字段名) from 表名;
max(字段名)
min(字段名)
分组查询
select 字段名1,字段名2.。。。。。 from 表名
group by 字段名1,字段名2,。。。。;
select 字段名1,count(*) from 表名
group by 字段名;
having子句
select 字段名1,字段名2,.... FROM 表名
group by 字段名1,字段名2,。。。[having条件表达式]
LIMIT分页
SELECT 字段名1,字段名,。。。。。。。 from 表名
limit [m,]n;
m默认为0, n表示取n条记录
mysql数据库学习笔记二
最新推荐文章于 2022-08-17 21:05:58 发布