语法:
SELECT
字段列表
FROM
表名字段
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后的条件列表
ORDER BY
排序字段列表
LIMIT
分页参数
查询多个字段:
SELECT 字段1, 字段2, 字段3, ... FROM 表名;
SELECT * FROM 表名;
设置别名:
SELECT 字段1 [ AS 别名1 ], 字段2 [ AS 别名2 ], 字段3 [ AS 别名3 ], ... FROM 表名;
SELECT 字段1 [ 别名1 ], 字段2 [ 别名2 ], 字段3 [ 别名3 ], ... FROM 表名;
去除重复记录:
SELECT DISTINCT 字段列表 FROM 表名;
转义:
SELECT * FROM 表名 WHERE name LIKE '/_张三' ESCAPE '/'
/ 之后的_不作为通配符
语法:
SELECT 字段列表 FROM 表名 WHERE 条件列表;
条件:
比较运算符 | 功能 |
---|---|
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
= | 等于 |
<> 或 != | 不等于 |
BETWEEN … AND … | 在某个范围内(含最小、最大值) |
IN(…) | 在in之后的列表中的值,多选一 |
LIKE 占位符 | 模糊匹配(_匹配单个字符,%匹配任意个字符) |
IS NULL | 是NULL |
逻辑运算符 | 功能 |
---|---|
AND 或 && | 并且(多个条件同时成立) |
OR 或 || | 或者(多个条件任意一个成立) |
NOT 或 ! | 非,不是 |
例子:
-- 年龄等于30
select * from employee where age = 30;
-- 年龄小于30
select * from employee where age < 30;
-- 小于等于
select * from employee where age <= 30;
-- 没有身份证
select * from employee where idcard is null or idcard = '';
-- 有身份证
select * from employee where idcard;
select * from employee where idcard is not null;
-- 不等于
select * from employee where age != 30;
-- 年龄在20到30之间
select * from employee where age between 20 and 30;
select * from employee where age >= 20 and age <= 30;
-- 下面语句不报错,但查不到任何信息
select * from employee where age between 30 and 20;
-- 性别为女且年龄小于30
select * from employee where age < 30 and gender = '女';
-- 年龄等于25或30或35
select * from employee where age = 25 or age = 30 or age = 35;
select * from employee where age in (25, 30, 35);
-- 姓名为两个字
select * from employee where name like '__';
-- 身份证最后为X
select * from employee where idcard like '%X';
常见聚合函数:
函数 | 功能 |
---|---|
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
语法:
SELECT 聚合函数(字段列表) FROM 表名;
例:
SELECT count(id) from employee where workaddress = "广东省";
语法:
SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组后的过滤条件 ];
where 和 having 的区别:
例子:
-- 根据性别分组,统计男性和女性数量(只显示分组数量,不显示哪个是男哪个是女)
select count(*) from employee group by gender;
-- 根据性别分组,统计男性和女性数量
select gender, count(*) from employee group by gender;
-- 根据性别分组,统计男性和女性的平均年龄
select gender, avg(age) from employee group by gender;
-- 年龄小于45,并根据工作地址分组
select workaddress, count(*) from employee where age < 45 group by workaddress;
-- 年龄小于45,并根据工作地址分组,获取员工数量大于等于3的工作地址
select workaddress, count(*) address_count from employee where age < 45 group by workaddress having address_count >= 3;
语法:
SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1, 字段2 排序方式2;
排序方式:
例子:
-- 根据年龄升序排序
SELECT * FROM employee ORDER BY age ASC;
SELECT * FROM employee ORDER BY age;
-- 两字段排序,根据年龄升序排序,入职时间降序排序
SELECT * FROM employee ORDER BY age ASC, entrydate DESC;
如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序
语法:
SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数;
例子:
-- 查询第一页数据,展示10条
SELECT * FROM employee LIMIT 0, 10;
-- 查询第二页
SELECT * FROM employee LIMIT 10, 10;
FROM -> WHERE -> GROUP BY -> SELECT -> ORDER BY -> LIMIT
create table employee(
id int comment'编号',
workno varchar(10) comment'工号',
name varchar(10) comment'姓名',
gender char(1) comment'性别',
age tinyint unsigned comment'年龄',
idcard char(18) comment'身份证号',
workaddress varchar(50) comment'工作地址',
entrydate date comment'入职时间'
)comment'员工表';
insert into employee(id, workno, name, gender, age, idcard, workaddress, entrydate)
values (1,'1','柳岩','女',20,'123456789012345678','北京','2000-01-01'),
(2,'2','张无忌','男',18,'123456789012345670','北京','2005-09-01'),
(3,'3','韦一笑','男',38,'123456789712345670','上海','2005-08-01'),
(4,'4','赵敏','女',18,'123456757123845670','北京','2009-12-01'),
(5,'5','小昭','女',16,'123456769012345678','上海','2007-07-01'),
(6,'6','杨道','男',28,'12345678931234567X','北京','2006-01-01'),
(7,'7','范瑶','男',40,'123456789212345670','北京','2005-05-01'),
(8,'8','黛绮丝','女',38,'123456157123645670','天津','2015-05-01'),
(9,'9','范凉凉','女',45,'123156789012345678','北京','2010-04-01'),
(10,'10','陈友谅','男',53,'123456789012345670','上海','2011-01-01'),
(11,'11','张士诚','男',55,'123567897123465670','江苏','2015-05-01'),
(12,'12','常遇春','男',32,'123446757152345670','北京','2004-02-01'),
(13,'13','张三丰','男',88,'123656789012345678','江苏','2020-11-01'),
(14,'14','灭绝','女',65,'123456719012345670','西安','2019-05-01'),
(15,'15','胡青牛','男',70,'12345674971234567X','西安','2018-04-01'),
(16,'16','周芷若','女',18,null,'北京','2012-06-01');
-- 查询指定字段
select name,workno,age from employee;
-- 查询所有字段并返回
select id, workno, name, gender, age, idcard, workaddress, entrydate from employee;
-- 上面的写法太麻烦了,我们这里使用通配符来搞定,但是可读性太差了
select * from employee;
-- 查询所用员工的工作地址,起别名
select employee.workaddress as '工作地址' from employee;
select employee.workaddress '工作地址' from employee;
-- 查询员工上班地址,并且去重
select distinct employee.workaddress '工作地址' from employee;
-- 条件查询 一共有11个案例
-- 年龄等于88
select * from employee where age=88;
-- 年龄小于20
select *from employee where age<20;
-- 年龄小于等于20
select * from employee where age <= 20;
-- 没有身份证号的员工
select * from employee where idcard is null;
-- 有身份证号的员工
select * from employee where idcard is not null;
-- 年龄不等于88的员工
select * from employee where age != 88;
select * from employee where age <> 88;
-- 年龄在15-20岁
select * from employee where age>= 15 && age <=20;
select * from employee where age>= 15 and age <=20;
select * from employee where age between 15 and 20;
-- 女生小于25
select * from employee where gender = '女' and age < 25;
-- 年龄等于18或者20或者40
select * from employee where age =18 or 20 or 40;
select * from employee where age in (18,20,40);
-- 查询姓名是两个字的员工
select * from employee where name like '__';
-- 身份证号最后一位是X的员工
select * from employee where idcard like '%X';
-- 聚合函数
-- 统计员工数量
select count(*) from employee;
select count(idcard) from employee;-- 这里的null不参与聚合函数计算!!!
select count(id) from employee;
-- 统计平均年龄
select avg(age) from employee;
-- 最大年龄
select max(age) from employee;
-- 最小年龄
select min(age) from employee;
-- 西安地区员工的年龄之和
select sum(age) from employee where workaddress = '西安';
-- 分组查询
-- 根据性别分组 并统计数量
select gender , count(*) from employee group by gender;
-- 根据性别分组 并计算平均年龄
select employee.gender , avg(age) from employee group by gender;
-- 年龄小于45,根据工作地点分组,获取员工数量大于3的工作地址
select workaddress, count(*) from employee where age < 45 group by workaddress ;
select workaddress, count(*) from employee where age < 45 group by workaddress having count(*) >= 3;
-- 排序查询
-- 根据年龄对公司的员工进行升序排序 降序
select * from employee order by age ASC;-- ASC可以省略
select * from employee order by age DESC;
-- 根据入职时间进行降序排序
select * from employee order by entrydate DESC;
-- 根据年龄对公司员工进行升序排序 年龄相同 按照入职时间进行降序排序
select * from employee order by age ASC ,entrydate DESC;
-- 分页查询
-- 查询第一页员工数据,每页展示10条记录
select * from employee limit 0,10;
select * from employee limit 10;
-- 查询第二页员工数据,每页展示10条记录 ---->(页码-1)*页展示记录数
select * from employee limit 10,10;
-- DQL语句练习
-- 年龄为20 21 22 23 的女性
select * from employee where gender = '女' and age in(20,21,22,23);
-- 男 20-40岁 并且名字为3个字
select * from employee where gender = '男' and (age between 15 and 40) and name like '___';
-- 年龄小于60岁的 男性员工和女性员工的数量
select gender, count(*) from employee where age < 60 group by gender;
-- 查询满足条件员工的姓名和年龄,年龄小于等于35 对查询结果进行年龄升序排序 然后按照入职时间降序排序
select name , age from employee where age <=35 order by age , entrydate desc;
-- 查询性别为男 年龄在20-40的前五个员工 对查询结果进行年龄升序排序 然后按照入职时间降序排序
select * from employee where gender = '男' and age between 20 and 40 order by age , entrydate desc limit 5;