数据查询语言
1、基础查询
2、条件查询
3、聚合函数
4、分组查询
5、分页查询
6、案例
7、执行顺序
select 字段列表
from 表名列表
where 条件列表
group by 分组字段列表
having 分组后条件列表
order by 排序字段列表
limit 分页参数
1、基础查询
select 字段1[as 别名1],字段2[as 别名2]... from 表名
select * from 表名 # 全部字段
select distinct 字段列表 from 表名 # 去除重复记录
insert into employee values(5,'67','人5','男',19,'123456789123456789','2019-09-10'),
(6,'69','人6','女',19,'123456789123456789','2019-09-10'),
(7,'61','人7','男',19,'123456789123456789','2019-09-10'),
(8,'62','人8','男',19,'123456789123456789','2019-09-10'),
(9,'64','人4','女',19,'123456789123456789','2019-09-10'),
(10,'65','人4','男',19,'123456789123456789','2019-09-10'),
(11,'66','人4','男',19,'123456789123456789','2019-09-10');
insert into employee values(12,'67','人5','女',19,'12345678912345678X','2019-09-10');
# 查询全部字段
select * from employee;
# 缺点:*不直观,影响开发效率
# 查询指定字段
select name,gender from employee;
# 查询字段取别名
select name as '姓名' from employee;
# 去重
select distinct name as'去重姓名' from employee;
2、条件查询
select name from employee where gender = '女';
select name from employee where age >= 19;
select name from employee where idcard is null;
select name from employee where idcard is not null;
select name from employee where gender != '女';
select name from employee where age != 18;
select name from employee where age <> 18;
select name from employee where age>=15 && age<=20;
select name from employee where age>=15 and age<=20;
select name from employee where age between 15 and 20; #闭集
select name from employee where gender='女' and age<=20;
select name from employee where age=20 or age=19;
select name from employee where name in ('人1','人4');
select * from employee where id like '__'; # 模糊匹配,两个字符的id
select * from employee where idcard like '%X'; # 身份证号末尾为X
select * from employee where idcard like '_________________X'; # 17个下划线
3、聚合函数
# 对一列数据纵向计算
select count(*) from employee;# 计算数量
select count(id) from employee;# 计算数量
# 注: null 不参与计算
select avg(age) from employee;# 平均数
select max(id) from employee;# 最大
select min(id) from employee;# 最小
select sum(age) from employee where gender='男';
select age from employee where gender='男';
4、分组查询
# where分组之前过滤;having分组之后过滤
# where不能对聚合函数进行判断 having可以
# 在使用 GROUP BY 子句时,所有 SELECT 列必须是聚合函数的结果或是 GROUP BY 中的列
# 执行顺序:where > 聚合函数 > having
select gender,count(age) from employee where age>18 group by gender;
select gender,max(age) from employee group by gender;
select gender,count(*) from employee group by gender;
select gender,count(id) from employee group by gender;
select gender,avg(age) from employee group by gender ;
select gender,count(gender) from employee where age >18 group by gender having count(gender)>4;
select gender,count(*) from employee where age >18 group by gender having count(*)>3;
5、分页查询
# 起始索引从0开始, 起始索引=(查询页码-1)*每页显示记录数
# 第一页可以省略起始索引
# 分页是方言, mysql是limit, oracle是row number
select * from employee limit 10,5; # 每页5条记录, 查询第3页
select * from employee limit 5; # 每页5条记录, 查询第1页
6、案例
select * from employee where gender = '女' and workno in(65,66,67);
select * from employee where gender = '男' and age between 18 and 20 and name like '__';# 名字中含有两个字的男性,年龄在18-20
select gender,count(*) from employee where age >18 group by gender ;
select name,age from employee where age >=18 order by age, entrydate desc ;
select * from employee where gender = '男' and age between 18 and 20 order by age,entrydate limit 5;# 男 18-20 前5个人,按年龄升序入职时间升序排序
7、顺序
# 编写顺序 select from where group by having order by limit
# 执行顺序 from——where——group by——having——select——order by——limit
select name,age from employee where age>15 order by age; # 15岁以上的信息,根据年龄升序排序
select name,age from employee e where age>15 order by age;
select e.name,e.age from employee e where e.age>15 order by age;
select e.name ename,e.age eage from employee e where e.age>15 order by age;
select e.name ename,e.age eage from employee e where e.age>15 order by eage;
# 其中 e是表的别名; ename,eage是字段的别名.where不能使用eage,是由于where的执行顺序在select前
# 关系型运算符优先级高到低为:NOT>AND>OR
# 题目:现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据
select device_id,gender,age,university,gpa from user_profile where gpa>3.5 and university='山东大学' or gpa>3.8 and university='复旦大学' ;