create database if not exists bilibili; #创建数据库bilibili use bilibili; #选择bilibili数据库,即可进行下面的代码演示 drop database if exists bilibili; #代码演示结束后删除bilibili数据库 show databases; #查看是否删除完成
-- 创建一个emp表格
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment '入职时间'
) comment '员工表';
-- 为emp表格的每一个字段添加数据
insert into emp (id,workno,name,gender,age,idcard,workaddress,entrydate)
values (1,'1','刘岩','女',20,'123456789012345670','北京','2000-01-01'),
(2,'2','张无忌','男',18,'123456789012345671','北京','2005-09-01'),
(3,'3','韦一笑','男',38,'123456789012345672','上海','2005-08-01'),
(4,'4','赵敏','女',18,'123456789012345673','北京','2009-12-01'),
(5,'5','小邵','女',16,'123456789012345674','上海','2007-07-01'),
(6,'6','杨瑶','男',28,'123456789012345675','北京','2006-01-01'),
(7,'7','范瑶','男',40,'123456789012345676','北京','2005-05-01'),
(8,'8','黛绮丝','女',38,'123456789012345677','天津','2015-05-01'),
(9,'9','范亮亮','女',45,'123456789012345678','北京','2010-04-01'),
(10,'10','陈友谅','男',53,'123456789012345679','上海','2011-01-01'),
(11,'11','张士诚','男',55,'12345678901234567X','江苏','2015-05-01'),
(12,'12','常遇春','男',32,'12345678901234568X','北京','2004-02-01'),
(13,'13','张三丰','男',88,'123456789012345671','江苏','2020-11-01'),
(14,'14','灭绝','女',65,'123456789012345672','西安','2019-05-01'),
(15,'15','胡青牛','男',70,'123456789012345673','西安','2018-04-01'),
(17,'17','临时员1','男',28,'123456789012345475','江苏','2006-05-01'),
(18,'18','临时员2','女',26,'123456789012345435','江苏','2006-09-01'),
(16,'16','周芷若','女',18,null,'北京','2012-06-01');
-- 一、 基础查询
-- 1. 查询指定字段 name,workno,age 并返回
select name,workno,age from emp;
-- 2. 查询所有字段并返回
select id,workno,name,gender,age,idcard,workaddress,entrydate from emp;
-- 3. 查询所有字段并返回,当字段比较多时,可使用通配符,但是实际开发中不建议使用,不符合开发规范
select * from emp;
-- 4. 查询所有员工的工作地址,起别名,这个别名会在列头的位置,取代原来的列头(列头也叫字段)
select workaddress as '工作地址' from emp;
-- 5. 查询所有员工的工作地址,起别名,as 可以省略
select workaddress '工作地址' from emp;
-- 6. 查询公司员工的上班地址(不要重复),即多个同一上班地址只输出其中一个
select distinct workaddress '工作地址' from emp;
-- 二、 条件查询
-- 1. 查询年龄等于 88 的员工
select * from emp where age = 88;
-- 2. 查询年龄小于 20 的员工信息
select * from emp where age < 20;
-- 3. 查询年龄小于等于 20 的员工信息
select * from emp where age <= 20;
-- 4. 查询年龄不等于 88 的员工信息,第一种写法
select * from emp where age != 88;
-- 5. 查询年龄不等于 88 的员工信息,第二种写法
select * from emp where age <> 88;
-- 6. 查询没有身份证号的员工信息
select * from emp where idcard is null;
-- 7. 查询有身份证号的员工信息
select * from emp where idcard is not null;
-- 8. 查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息,第一种写法
select * from emp where age >= 15 && age <= 20;
-- 9. 查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息,第二种写法
select * from emp where age >= 15 and age <= 20;
-- 10. 查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息,第三种写法
select * from emp where age between 15 and 20;
-- 11. 查询性别为 女 且年龄小于 25岁的员工信息
select * from emp where gender = '女' and age < 25;
-- 12. 查询年龄等于18 或 20 或 40 的员工信息,第一种写法
select * from emp where age = 18 or age = 20 or age = 40;
-- 13. 查询年龄等于18 或 20 或 40 的员工信息,第二种写法
select * from emp where age in(18,20,40);
-- 14. 查询姓名为两个字的员工信息,使用模糊匹配
select * from emp where name like '__';
-- 15. 查询身份证号最后一位是X的员工信息,使用模糊匹配,第一种写法
select * from emp where idcard like '_________________X';
-- 16. 查询身份证号最后一位是X的员工信息,使用模糊匹配,第二种写法
select * from emp where idcard like '%X';
-- 三、 聚合函数,注意所有的null值是不参与计算的
-- 1. 统计该企业员工数量,第一种写法
select count(*) from emp;
-- 2. 统计该企业员工数量,第二种写法,注意如果字段内容有一个是null,那这个null是不会算入计数的,望留意
select count(id) from emp; -- 结果是16
select count(idcard) from emp; -- 结果是15,因为idcast字段有一个员工的值是null,不计数
-- 3. 统计该企业员工的平均年龄
select avg(age) from emp;
-- 4. 统计该企业员工的最大年龄
select max(age) from emp;
-- 5. 统计该企业员工的最小年龄
select min(age) from emp;
-- 6. 查询一西安地区的员工有哪些
select * from emp where workaddress = '西安';
-- 7. 统计西安地区员工的年龄之和
select sum(age) from emp where workaddress = '西安';
-- 四、 分组查询, 通常配合聚合函数来操作
-- 1. 根据性别分组, 统计男性员工 和 女性员工的数量
select gender,count(*) from emp group by gender; -- 第一个gender是把性别也查出来,第二个是根据gender来分组
-- 2. 根据性别分组, 统计男性员工 和 女性员工的平均年龄
select gender,avg(age) from emp group by gender;
-- 3. 查询年龄小于45的员工, 并根据工作地址分组, 第一种写法
select workaddress,count(*) from emp where age < 45 group by workaddress;
-- 4. 查询年龄小于45的员工, 并根据工作地址分组,第二种写法
select workaddress,count(workaddress) from emp where age < 45 group by workaddress;
-- 5. 查询年龄小于45的员工, 并根据工作地址分组, 获取员工数量大于等于3的工作地址,也就是在分组的基础上再进行过滤
select workaddress,count(*) from emp where age < 45 group by workaddress having count(*) >= 3;
-- 6. 查询年龄小于45的员工, 并根据工作地址分组, 获取员工数量大于等于3的工作地址,为方便阅读需要给字段起别名,as可以省略
select workaddress,count(*) as address_count from emp where age < 45 group by workaddress having count(*) >= 3;
-- 五、 排序查询
-- 1. 根据年龄对公司的员工进行升序排序,asc可以省略,因为默认就是asc
select * from emp order by age asc;
-- 2. 根据年龄对公司的员工进行降序排序
select * from emp order by age desc ;
-- 3. 根据入职时间,对员工进行降序排序
select * from emp order by entrydate desc;
-- 4. 根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序。需要使用多字段排序,不难的
select * from emp order by age asc , entrydate desc;
-- 六、分页查询,不同的数据库有不同的关键字,在MySQL中是limit
-- 1. 查询第1页员工数据,每页展示10条数据,第一种写法(起始索引从0开始)
select * from emp limit 0,10;
-- 2. 查询第1页员工数据,每页展示10条数据,第二种写法(当查询第1页时,参数0可以省略)
select * from emp limit 10;
-- 3. 查询第2页员工数据,每页展示10条数据。起始索引 = (查询页码-1) * 每页显示记录数
select * from emp limit 10,10;
-- 七、DQL案例练习
-- 1. 查询年龄为20,21,22,23岁的员工信息
select * from emp where gender = '女' and age in(20,21,22,23);
-- 2. 查询性别为 男 ,并且年龄在20(含)-40岁(含)以内的姓名为3个字的员工。第一种写法
select * from emp where gender = '男' and age between 20 and 40 and name like '___';
-- 3. 查询性别为 男 ,并且年龄在20(含)-40岁(含)以内的姓名为3个字的员工。第二种写法
select * from emp where gender = '男' and ( age between 20 and 40 ) and name like '___';
-- 4. 查询性别为 男 ,并且年龄在20(含)-40岁(含)以内的姓名为3个字的员工。第三种写法
select * from emp where gender = '男' and age>=20 && age<=40 and name like '___';
-- 5. 统计emp表中,年龄小于60岁的,男性员工和女性员工的人数。性别进行分组,查询所有员工的男性和女性数量,查询之前先按照年龄过滤
select gender,count(*) from emp where age<60 group by gender;
-- 6. 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序。asc可省略,多字段排序
select name,age from emp where age<=35 order by age asc,entrydate desc;
-- 7. 查询性别为男,且年龄在20-40岁(含)以内的前五个员工信息(要用分页查询),对查询的结果按年龄升序排序,年龄相同按入职时间升序排序
select * from emp where gender = '男' and (age between 20 and 40) order by age asc,entrydate asc limit 0,5;
-- 注意在命令行输入括号不影响运算,只是方便直观的看代码 -- 注意如果同时有排序和分页,要先排序后分页,即order by写在limit前面,不然报错 -- 注意使用order by排序时,如果是升序则可以省略asc不写 -- 注意使用limit分页时,例如第一页的前五个数据limit 0,5 因为是第一页所以0可以省略不写 -- 注意使用limit分页时,如果最后筛选出来的数据只有4条,即使分页每页要求展示5条,在实际中只会显示4条 -- 注意如果查询员工信息就用*,查询其他就用对应字段名
-- 八、DQL执行顺序
-- 第一步、from 表名 -- 第二步、where 条件 -- 第三步、group by 分组字段 -- 第四步、having 分组后条件 -- 第五步、select 字段 -- 第六步、order by 排序字段 -- 第七步、limit 分页参数
-- 1. 查询年龄大于15的员工的姓名、年龄,并根据年龄进行升序排序
select name,age from emp where age>15 order by age asc;
-- 2. 在上面的基础上,添加一个别名,as可以省略。验证先from,再执行where。原理:先from生成表别名,才能在where后面用别名调用age
select name,age from emp as e where e.age>15 order by age asc;
-- 3. 在上面的基础上,继续使用这个别名,as可以省略。验证先from,再执行select。原理:先from生成表别名,才能在select后面用别名调用age和name
select e.name,e.age from emp as e where e.age>15 order by age asc;
-- 4. 在上面的基础上,给e.name起一个别名为ename,给e.age起一个别名为eage,where后面改为eage。报错,即可验证先where再执行select
select e.name ename,e.age eage from emp as e where eage>15 order by age asc;
-- 5. 在上面的基础上,把where后面改回age,把order by后面的age换成eage。不报错,即可验证先执行select,再执行order by
select e.name ename,e.age eage from emp as e where e.age>15 order by eage asc;
-- 6. 上面的几个代码演示可证明最初第1小点的SQL语句执行顺序为from -> where -> select -> order by