目录
条件查询
1.语法
select 字段列表 from 表名 where 条件列表;
2.条件
下列表中常用的比较运算符如下:
常用的逻辑运算符如下:
案例:
#条件查询
select *from emp01 where age<=20;
#查询没有/有身份号的员工信息
select * from emp02 where idcard is null;
select * from emp02 where idcard is not null;
# 查询年龄不等于 88 的员工信息
select * from emp02 where age != 88;
select * from emp02 where age <> 88;
#查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息
select * from emp02 where age >= 15 && age <= 20;
select * from emp02 where age >= 15 and age <= 20;
select * from emp02 where age between 15 and 20;
#查询性别为 女 且年龄小于 25岁的员工信息
select * from emp02 where gender = '女' and age < 25;
# 查询年龄等于18 或 20 或 40 的员工信息
select * from emp where age = 18 or age = 20 or age =40;
select * from emp where age in(18,20,40);
# 查询姓名为两个字的员工信息 _ %
select * from emp02 where name like '__';
#查询身份证号最后一位是X的员工信息
select * from emp02 where idcard like '%X';
select * from emp02 where idcard like '_________________X';
聚合函数
介绍
将一列数据作为一个整体 ,进行纵向计算
语法
select 聚合函数( 字段列表 ) from 表名
注意:null值不参与所有聚合函数计算
常见的聚合函数
案例:
#聚合函数
select * from emp02;
#统计该企业的员工数量
select count(*) from emp02;
select count(idcard) from emp02;
select count(1) from emp02;
#统计企业的平均年龄
select avg(age) from emp02;
#统计企业的最大/最小年龄
select max(age) from emp02;
select min(age) from emp02;
#统计西安地区员工年龄之和
select sum(age) from emp02 where workaddress ='西安';
分组查询
语法
select 字段列表 from 表明 (where 条件) group by 分组字段(having 分组后过滤的条件)
where 和 having 区别
- 执行时机不同:where是分组之前进行过滤,不满足where条件不参与分组;having 是分组之后对结果进行过滤
- 判断条件不同:where 不能对聚合函数进行判断,而having可以
注意事项:
- 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义
- 执行顺序:where > 聚合函数 > having
- 支持多字段分组,具体语法为:group by *** , ***;
案例:
#分组查询
#根据性别分组,统计男性员工和女性员工的数量
select gender, count(*) from emp02 group by gender;
#根据性别分组,统计男性员工和女性员工的平均年龄
select gender, avg(age) from emp02 group by gender;
#查询年龄小于45的员工 , 并根据工作地址分组 , 获取员工数量大于等于3的工作地址
select workaddress, count(*) from emp02 where age < 45 group by workaddress having count(*)>=3;
#统计各个工作地址上班的男性及女性员工的数量
select workaddress,gender,count(*) '数量' from emp02 group by gender,workaddress;
排序查询
排序在日常开发中是非常常见的一个操作,有升序排序,也有降序排序。
语法
select 字段列表 from 表名 order by 字段1 排序方式 , 字段2 排序方式2 ;
排序方式
- ASC: 升序(默认 可省略)
- DESC: 降序
注意事项
如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序
案例:
#排序查询
#根据年龄对公司的员工进行升序排序
select * from emp02 order by age asc;
#根据入职时间, 对员工进行降序排序
select * from emp02 order by entrydate desc;
#根据年龄对公司的员工进行升序排序 , 年龄相同 , 再按照入职时间进行降序排序
select * from emp02 order by age asc,entrydate desc;
分页查询
分页操作在业务系统开发时,也是非常常见的一个功能,我们在网站中看到的各种各样的分页条,后台都需要借助于数据库的分页操作。
语法
select 字段列表 from 表名 limit 起始索引,查询记录数;
注意事项:
- 起始索引从0开始,起始索引 = (查询页码-1) * 每页显示记录数
- 分页查询是数据库的方言,不同数据库有不同的实现,MySQL中是limit
- 如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10
案例:
#分页查询
#查询第1页员工数据, 每页展示10条记录
select * from emp02 limit 0,10;
#查询第1页员工数据, 每页展示10条记录
select * from emp02 limit 10,10;
去重查询
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录。
不过得注意的是distinct关键字得放在查询字段前面
select distinct university from user_profile;
一般distinct用来查询不重复记录的条数。(指不重复的种类名称)
如果要查询不重复的记录,有时候可以用group by (指不重复的种类的数量)
综合案例:
#查询年龄为20,21,22,23岁的女员工信息
select * from emp02 where gender='女' and age in(20,21,22,23);
#查询性别为 男 ,并且年龄在 20-40 岁(含)以内的姓名为三个字的员工。
select * from emp02 where gender='男' and (age between 20 and 40) and name like '___';
# 统计员工表中, 年龄小于60岁的 , 男性员工和女性员工的人数
select gender,count(*) from emp02 where age<60 group by gender;
#查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序。
select name,age from emp02 where age<=35 order by age asc,entrydate desc;
#查询性别为男,且年龄在20-40 岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序。
select * from emp02 where gender='男' and age between 20 and 40 order by age asc,entrydate asc limit 0,5;
附 编写顺序和执行顺序