DQL-查询数据
- DQL-语法
select 字段列表 from 表名 where 条件列表 group by 分组字段列表 having 分组后条件列表 order by 排序字段列表 limit 分页参数
-
基本查询
1.查询指定字段 name,age,sex 返回select name, age, sex from employee;
2.查询所有字段返回
select * from employee;
尽量少用*,而是用字段代替3.查询所有员工的工作地址,起别名
select workaddress as ‘别名’ from 表名;
举例:select workaddress as ‘公司地址’ from employee;
as 可以省略4.查询数据,去除重复记录
select distni from employee
-
条件查询
- 语法
select 字段列表 from 表名 where 条件列表;
-
条件
-
举例
-
查询年龄等于88的员工
select 字段列表 from employee where age = 88;
-
查询年龄小于20的员工信息
select 字段列表 from employee where age < 20;
-
查询没有身份证号的员工信息
select 字段列表 from employee where idcard is null;
-
查询年龄在15(包含)到22(包含)之间的员工
select 字段列表 from employee where age <= 22 and age >= 15;
select 字段列表 from employee where age <= 22 && age >= 15;
select 字段列表 from employee where age between 15 and 22;
-
查询年龄等于18或20或22的员工
select * from employee where age = 18 or age = 20 or age = 22;
select * from employee where age in (18, 20, 22);
-
查询姓名为两个字的员工信息(需要用到like 模糊查询)
select * from employee where name like '__';
-
查询身份证号最后一位是x的员工信息
select * from employee where idcard like '%x';
-
聚合函数
-
常见聚合函数
-
语法
select 聚合函数(字段列表) from 表名;
字段值为null值不参与聚合函数计算- 举例
-
统计企业员工数量
select count(*) from emp;
-
统计企业员工的平均年龄
select avg(age) from emp;
-
统计企业员工的最大年龄
select max(age) from emp;
-
统计企业员工的最小年龄
select min(age) from emp;
-
统计企业西安地区员工的年龄之和
select sum(age) from emp where workaddress = '西安';
-
-
分组查询
- 语法
select 字段列表 from 表名 【where 条件】 group by 分组字段名 【HAVING 分组后过滤条件】;
- where与having区别
- 判断时机不同:where是分组之前进行过滤,不满足where条件不参与分组;而having是分组之后对结果进行过滤
- 判断条件不同:where不能对聚合函数进行判断,而having可以
- 举例
-
根据性别分组,统计男性员工和女性员工的数量
select sex, count(*) from emp group by sex;
-
根据性别分组,统计男性员工和女性员工的平均年龄
select sex, avg(age) from emp group by sex;
-
查询年龄小于45的员工,并根据工作地址分组,获取分组员工数量大于3人工作地址
select workaddress, count(*) address_count from emp where age < 45 group by workaddress having count(*) >= 3;
代码解析:
查询年龄小于45的员工,并根据工作地址分组
select workaddress from emp where age < 45 group by workaddress;查询年龄小于45的员工,并根据工作地址分组,获取分组员工数量
select workaddress,count(*) from emp where age < 45 group by workaddress;查询年龄小于45的员工,并根据工作地址分组,获取分组员工数量并起别名
select workaddress,count(*) workaddress from emp where age < 45 group by workaddress;查询年龄小于45的员工,并根据工作地址分组,获取分组员工数量(起别名)大于3人的工作地址
select workaddress,count(*) workaddress from emp where age < 45 group by workaddress having workaddress > 3;注意:
- 执行顺序:where > 聚合函数 > having
- 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义
-
排序查询
- 语法
select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;
- 排序方式
- asc: 升序(默认)
- desc: 降序
- 举例
-
根据年龄对公司员工进行升序排序(升序asc可以省略)
select * from emp order by age asc;
-
根据入职时间对员工进行降序排序
select * from emp order by entrydate desc;
-
根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序
select * from emp order by age, entrydate desc;
-
分页查询
- 语法
select 字段列表 from 表名 limit 起始索引,查询记录数;
注意:
- 起始索引从0开始,起始索引=(查询页码-1)*每页记录数
- 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是limit
- 如果查询的第一页数据,起始索引可以省略,直接简写为limit 10
- 举例
-
查询第一页的员工数据,每页展示10条记录
select * from emp limit 0, 10;
select * from emp limit 10;
-
查询第二页员工数据,每页展示10条记录
select * from emp limit 1, 10;
-
DQL-执行顺序
-
案例练习
-
查询所有员工年龄小于35岁的员工姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序
select name, age from emp where age < 35 order by age esc, entrydate desc;
-
查询性别为男,且年龄在20-40岁(含)之间的前5个员工信息,对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序
select * from emp where sex = '男' and (age between 20 and 40) order by age, entrydate desc limit 5;
-