select * from student
insert into student(name,sex,age,tel,height,city)
values(‘小红‘,‘女‘,20,‘13111111111‘,1.68,‘湘潭‘)
insert into student
values(null,‘小明‘,‘男‘,22,‘13222222222‘,1.73,‘长沙‘)
insert into student
select ‘null‘,‘小军‘,‘男‘,‘27‘,‘13333333333‘,1.74,‘株州‘ union
select ‘null‘,‘小晶‘,‘女‘,‘24‘,‘13444444444‘,1.66,‘长沙‘ union
select ‘null‘,‘小黄‘,‘男‘,‘25‘,‘13555555555‘,1.71,‘湘潭‘ union
select ‘null‘,‘小朱‘,‘男‘,‘26‘,‘13666666666‘,1.71,‘长沙‘ union
select ‘null‘,‘小婷‘,‘女‘,‘27‘,‘13777777777‘,1.75,‘岳阳‘
select * from student
select name as‘姓名‘,age as‘年龄‘,tel as‘电话‘ from student
select * from student where name like ‘%三%‘
select * from student where age between 20 and 30
and tel like ‘138%‘ <br>
select * from student where tel like ‘_3%9‘
select * from student where id in(1,2,4,5)
select * from student where tel is null and city is not null
select * from student where sex=‘男‘ order by age asc
注:聚合函数是用来针对列的所有值操作,只返回一个结果
聚合函数不能与其它列名一起来进行查询操作,除非分组
select max(age) as‘最大值‘ from student
`select min(height) as‘最低身高‘,max(height) as‘最高身高‘ `<br>
`from student`
select avg(age) from student
select sum(age) from student
select count(id) from student
select count(*) from student -- 效率低于count(id)
select
count(id) as‘总人数‘,
min(age) as‘最小年龄‘,
max(age) as‘最大年龄‘,
avg(age) as‘平均年龄‘,
max(age)-min(age) as‘最大差距‘
from student
注:分组通常都会与聚合函数一起使用
以哪个列分组就需要显示哪个列
分组前的条件是 where ... group by ...
分组后的条件是 group by 列名 having 聚合条件
select sex as‘性别‘,count(id) as‘人数‘
from student
group by sex
select city as‘城市‘,count(id) as‘人数‘
from student
group by city
select city as‘城市‘,count(id) as‘人数‘
from student
where sex=‘男‘
group by city
select city as‘城市‘,count(id) as‘人数‘
from student
where sex=‘男‘
group by city
having count(id)>=3
select city as‘城市‘,avg(age) as‘平均年龄‘
from student
where sex=‘女‘
group by city
原文:https://www.cnblogs.com/xiaosublog/p/14763550.html