mysql查询

mysql 查询 DQL语句

条件查询 > ,< ,!=, and ,or, in, is null , is not null

select * from student;
-- 查询年龄大于20的
select * from student where age>=20;
-- 查询年龄不等于20的
select * from student where age!=20;-- 或者是select * from student where age <> 20;
-- 查询年龄大于20 小于30    推荐 	and并且的意思 &&不推荐
select * from student where age>=20 and age<=30;
-- 等效于 
select * from student where between 20 and 30;
-- 查询22岁 19岁  25岁的人的信息 or 或者
select * from student where age=22 or age=19 or age=25;
-- 等效于	in 
select * from student where age in (19,22,25);
-- 查询age为空的 null 不能用=
select * from student where age is null;
-- 查询age不为空的
select * from student where age is not null;

模糊查询 like

1._占1个字符
2.%占多个字符
-- 基本语法  select * from student name like "";
 select * from student name like "_忠%";
-- 查询包含字符
select * from student name like "%忠%";

排序查询 order by 不写默认是升序 asc 降序 desc

-- 基本语法  select * from student order by 字段1,字段2;
 select * from student order by score desc;

聚合函数 sum 求和 max 最大值 min 最小值 count统计数量 avg 平均值

注意:聚合函数会排除null值不会计算
-- 基本语法 select 函数名(字段) from student ;
	select count(*) from student ;
	select sum (age) from student ;
	select max (age) from student ;
	select min (age) from student ;
	select avg (age) from student ;

分组查询 group by

注意:分组之后查询的字段要么是分组的字段 要么是聚合函数
where 与having的区别
where是在分组之前进行限定,如果不满足条件,则不参与分组,having在分组之后进行限定,如果不满足条件,则不会被查询出来
-- 基本语法  select 分组的字段 或者是聚合函数 from student group by 分组的字段;
select sex,sum(score) from student group by sex; 
-- 分数低于70分不参与分组
select sex,sum(score) from student where score >=70 group by sex; 
-- 分组之后人数大于2个
select sex,sum(score),cound(*) from student where score >=70 group by sex having cound(*)>2;

分页查询 limit 数据开始的索引 (n-1)*每页显示的条数;

-- 基本语法 select *  from student limit 数据开始的索引,每页显示的条数; 
	select *  from student limit 0,3; -- 第一页
	select *  from student limit 3,3; -- 第二页
	select *  from student limit (n-1)*3,3; -- 第n页
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值