DQL英文全称Data Query Language,即数据查询语言,用来查询表中数据
完整语法如下:
select 字段列表 from 表名列表 where 条件列表 group by 分组字段列表 having 分组后条件列表 order by 排序字段列表 limit 分页参数
mysql> select id,username from emp;
+------+----------+
| id | username |
+------+----------+
| 1 | Richie |
| 2 | Taylor |
| 3 | Mike |
+------+----------+
3 rows in set (0.00 sec)
mysql> select * from emp; # 数据量大的时候尽量不要使用,因为效率比较低
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 3 | Mike | 45 | 男 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
设置别名的好处在于,我们可以自定义字段名,可以更直观的观察数据
mysql> select username as name,gender as sex from emp;
+--------+------+
| name | sex |
+--------+------+
| Richie | 男 |
| Taylor | 女 |
| Mike | 男 |
+--------+------+
3 rows in set (0.00 sec)
mysql> select username name,gender sex from emp; # as可以省略
+--------+------+
| name | sex |
+--------+------+
| Richie | 男 |
| Taylor | 女 |
| Mike | 男 |
+--------+------+
3 rows in set (0.00 sec)
mysql> select distinct gender '性别' from emp; # 使用关键字distinc来去除重复记录
+------+
| 性别 |
+------+
| 男 |
| 女 |
+------+
2 rows in set (0.00 sec)
比较运算符 | 功能 |
---|---|
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
= | 等于 |
<> 或 != | 不等于 |
between … and … | 在某个范围之内(含最大最小值) |
in(…) | 在in之后的列表中的值,就是多选一的意思 |
like 占位符 | 模糊匹配(_匹配单个字符,%匹配任意个字符) |
is null | 是null |
逻辑运算符 | 功能 |
---|---|
and 或 && | 并且(多个条件同时成立) |
or 或 || | 或者(多个条件任意一个成立) |
not 或 ! | 非(不是) |
mysql> select * from emp where age = 88; # 查询年龄等于88的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 5 | 张三 | 88 | |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where age < 30; # 查询年龄小于30的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from emp where age <= 25; # 查询年龄小于等于25的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from emp where username = '' or username is null; # 查询没有姓名的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 4 | | 25 | 女 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where not (username = '' or username is null); # 查询有姓名的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 3 | Mike | 45 | 男 |
| 5 | 张三 | 88 | |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp where age != 45; # 查询年龄不等于45的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 4 | | 25 | 女 |
| 5 | 张三 | 88 | |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp where age between 20 and 40; # 查询年龄在20-40之间的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
mysql> select * from emp where gender = '女' and age < 30; # 查询性别为女且年龄小于30的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 4 | | 25 | 女 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where gender = '女' and age < 30; # 查询年龄为21或者25或者34的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 4 | | 25 | 女 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where age in (21,25,34); # 查询年龄为21或者25或者34的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
mysql> select * from emp where username like '__'; # 查询姓名为两个字的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 5 | 张三 | 88 | |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where username like '%e'; # 查询姓名最后是e的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 3 | Mike | 45 | 男 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
聚合函数用于将一列数据作为一个整体,进行纵向的计算
注意:null不参与聚合函数的运算
函数 | 功能 |
---|---|
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 3 | Mike | 45 | 男 |
| 4 | | 25 | 女 |
| 5 | 张三 | 88 | |
+------+----------+------+--------+
5 rows in set (0.00 sec)
mysql> select count(id) from emp; # 统计有多少条包含id字段的记录,不统计null
+-----------+
| count(id) |
+-----------+
| 5 |
+-----------+
1 row in set (0.00 sec)
mysql> select avg(age) from emp; # 统计员工的平均年龄
+----------+
| avg(age) |
+----------+
| 42.6000 |
+----------+
1 row in set (0.03 sec)
mysql> select max(age) from emp; # 统计员工的最大年龄
+----------+
| max(age) |
+----------+
| 88 |
+----------+
1 row in set (0.00 sec)
mysql> select min(age) from emp; # 统计员工的最小年龄
+----------+
| min(age) |
+----------+
| 21 |
+----------+
1 row in set (0.00 sec)
mysql> select sum(age) from emp where gender = '男'; # 统计男员工的年龄之和
+----------+
| sum(age) |
+----------+
| 66 |
+----------+
1 row in set (0.00 sec)
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤的条件]
注意:执行顺序为 where > 聚合函数 > having
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
| 5 | Joker | 56 | 男 |
| 6 | Nancy | 18 | 女 |
+------+----------+------+--------+
6 rows in set (0.00 sec)
mysql> select gender,count(*) from emp group by gender; # 根据性别分组,统计男员工和女员工的数量
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 4 |
| 女 | 2 |
+--------+----------+
2 rows in set (0.00 sec)
mysql> select gender,avg(age) from emp group by gender; # 根据性别分组,统计男员工和女员工的平均年龄
+--------+----------+
| gender | avg(age) |
+--------+----------+
| 男 | 50.7500 |
| 女 | 23.5000 |
+--------+----------+
2 rows in set (0.03 sec)
mysql> select gender,count(*) from emp where age < 60 group by gender having count(*) >= 3; # 查询年龄小于60的员工,并根据性别分组,获取员工数量大于等于3的性别
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 3 |
+--------+----------+
1 row in set (0.00 sec)
mysql> select gender,count(*) num from emp where age < 60 group by gender having num >= 3; # 还可以在上一个例题中起别名
+--------+-----+
| gender | num |
+--------+-----+
| 男 | 3 |
+--------+-----+
1 row in set (0.03 sec)
select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2 ... ;
排序方式
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
| 5 | Joker | 56 | 男 |
| 6 | Nancy | 18 | 女 |
+------+----------+------+--------+
6 rows in set (0.00 sec)
mysql> select username '姓名',age '年龄',gender '性别' from emp where gender = '男' order by age; # 根据 年龄对男员工进行升序排序
+--------+------+------+
| 姓名 | 年龄 | 性别 |
+--------+------+------+
| Richie | 21 | 男 |
| Mike | 38 | 男 |
| Joker | 56 | 男 |
| Jack | 88 | 男 |
+--------+------+------+
4 rows in set (0.00 sec)
select 字段列表 from 表名 limit 起始索引 , 查询记录数;
注意:
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
| 5 | Joker | 56 | 男 |
| 6 | Nancy | 18 | 女 |
| 7 | Linken | 57 | 男 |
+------+----------+------+--------+
7 rows in set (0.00 sec)
mysql> select * from emp limit 0 , 4; # 查询第一页员工数据,每页展示4条数据
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp limit 4; # 查询第一页数据时可以简写
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp limit 5,5; # 查询第二页数据,每页展示5条数据
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 6 | Nancy | 18 | 女 |
| 7 | Linken | 57 | 男 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
# 查询年龄为20,21,22,23岁的女性员工信息
select * from emp where age in (20,21,22,23) && gender = '女';
# 查询性别为男,并且年龄在 20-40 (不包含20岁包含40岁)岁且姓名长度等于4的员工信息
select * from emp where gender = '男' && age between 21 and 40 && name like '____';
# 统计员工表中年龄小于60岁的男性员工和女性员工的人数
select gender , count(*) from emp where age < 60 group by gender
# 查询所有年龄小于等于35岁的员工姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序
select name,age from emp where age <= 35 order by age asc,entrytime desc;
# 查询性别为男,并且年龄在 20-40岁(包含20、40岁)以内的前五名员工信息
select * from emp where gender = '男' && age between 20 and 40 limit 5;