表格创建:
-- 员工管理(带约束)
create table tb_emp
(id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用户名',password varchar(32) default '123456' comment '密码',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',image varchar(300) comment '图像',job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管',entrydate date comment '入职时间',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间'
) comment '员工表';
数据准备:
-- 准备测试数据
INSERT INTO tb_emp (id, username, password, name, gender, image, job, entrydate, create_time, update_time)
VALUES (1, 'jinyong', '123456', '金庸', 1, '1.jpg', 4, '2000-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:35'),(2, 'zhangwuji', '123456', '张无忌', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:37'),(3, 'yangxiao', '123456', '杨逍', 1, '3.jpg', 2, '2008-05-01', '2022-10-27 16:35:33', '2022-10-27 16:35:39'),(4, 'weiyixiao', '123456', '韦一笑', 1, '4.jpg', 2, '2007-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:41'),(5, 'changyuchun', '123456', '常遇春', 1, '5.jpg', 2, '2012-12-05', '2022-10-27 16:35:33','2022-10-27 16:35:43'),(6, 'xiaozhao', '123456', '小昭', 2, '6.jpg', 3, '2013-09-05', '2022-10-27 16:35:33', '2022-10-27 16:35:45'),(7, 'jixiaofu', '123456', '纪晓芙', 2, '7.jpg', 1, '2005-08-01', '2022-10-27 16:35:33', '2022-10-27 16:35:47'),(8, 'zhouzhiruo', '123456', '周芷若', 2, '8.jpg', 1, '2014-11-09', '2022-10-27 16:35:33', '2022-10-27 16:35:49'),(9, 'dingminjun', '123456', '丁敏君', 2, '9.jpg', 1, '2011-03-11', '2022-10-27 16:35:33', '2022-10-27 16:35:51'),(10, 'zhaomin', '123456', '赵敏', 2, '10.jpg', 1, '2013-09-05', '2022-10-27 16:35:33', '2022-10-27 16:35:53'),(11, 'luzhangke', '123456', '鹿杖客', 1, '11.jpg', 2, '2007-02-01', '2022-10-27 16:35:33','2022-10-27 16:35:55'),(12, 'hebiweng', '123456', '鹤笔翁', 1, '12.jpg', 2, '2008-08-18', '2022-10-27 16:35:33', '2022-10-27 16:35:57'),(13, 'fangdongbai', '123456', '方东白', 1, '13.jpg', 1, '2012-11-01', '2022-10-27 16:35:33','2022-10-27 16:35:59'),(14, 'zhangsanfeng', '123456', '张三丰', 1, '14.jpg', 2, '2002-08-01', '2022-10-27 16:35:33','2022-10-27 16:36:01'),(15, 'yulianzhou', '123456', '俞莲舟', 1, '15.jpg', 2, '2011-05-01', '2022-10-27 16:35:33','2022-10-27 16:36:03'),(16, 'songyuanqiao', '123456', '宋远桥', 1, '16.jpg', 2, '2010-01-01', '2022-10-27 16:35:33','2022-10-27 16:36:05'),(17, 'chenyouliang', '12345678', '陈友谅', 1, '17.jpg', null, '2015-03-21', '2022-10-27 16:35:33','2022-10-27 16:36:07'),(18, 'zhang1', '123456', '张一', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:09'),(19, 'zhang2', '123456', '张二', 1, '2.jpg', 2, '2012-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:11'),(20, 'zhang3', '123456', '张三', 1, '2.jpg', 2, '2018-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:13'),(21, 'zhang4', '123456', '张四', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:15'),(22, 'zhang5', '123456', '张五', 1, '2.jpg', 2, '2016-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:17'),(23, 'zhang6', '123456', '张六', 1, '2.jpg', 2, '2012-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:19'),(24, 'zhang7', '123456', '张七', 1, '2.jpg', 2, '2006-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:21'),(25, 'zhang8', '123456', '张八', 1, '2.jpg', 2, '2002-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:23'),(26, 'zhang9', '123456', '张九', 1, '2.jpg', 2, '2011-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:25'),(27, 'zhang10', '123456', '张十', 1, '2.jpg', 2, '2004-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:27'),(28, 'zhang11', '123456', '张十一', 1, '2.jpg', 2, '2007-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:29'),(29, 'zhang12', '123456', '张十二', 1, '2.jpg', 2, '2020-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:31');
查询代码(根据属性筛选查询):
-- 查询username name属性
select username,name from tb_emp;-- 查询该表的所有信息select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp;select * from tb_emp;-- 查询name gender 并取别名 命名为 姓名 性别
select name as '姓名',gender as '性别' from tb_emp;-- 查询有几个工作职位 要求不重复select distinct job from tb_emp;
条件查询:
-- 查询job为null的员工
select name,job from tb_emp where job is null;-- 查询id <= 5的员工
select id,name,gender,job from tb_emp where id <= 5;
DQL分组查询(聚合函数 sum、min、max、avg):
-- ================DQL分组查询=================
-- 聚合函数:不统计null
-- 使用count函数时需要避免统计可以为null的标签-- 统计有几名员工
select count(id) from tb_emp;-- 查询还未分配工作的人员
select count(id) from tb_emp where job is null;-- 计算id的平均值
select avg(id) from tb_emp;-- 查询入职时间最长的员工 -min
select min(entrydate) from tb_emp;-- 查询入职时间最晚的员工 -max
select max(entrydate) from tb_emp;-- 计算id之和 -sum
select sum(id) from tb_emp;
分组查询(group by):
-- 根据性别分组 同时统计各个性别的数量
select gender,count(*) from tb_emp group by gender;-- 统计入职时间在'2015-01-01'之前的员工,并根据职位进行分类 统计员工数量大于2的职位
-- where 是在对分组前进行分类,不满足条件的不进行分类 having是对分类后的数据进行过滤
select job,count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*) >= 2;
排序查询(asc为默认升序排序, desc降序排序):
-- =======排序查询=======
-- 默认为升序排序 asc可以省略
-- asc: ascending order 升序排序
-- desc: descending order 降序排序-- 根据入职时间进行升序排序
select name,entrydate from tb_emp order by entrydate asc;-- 根据入职时间进行升序排序 入职时间相同按照更新时间进行降序排序
select name,entrydate,update_time from tb_emp order by entrydate asc,update_time desc ;
分页查询(limit):
-- =======分页查询========
-- 查询起始位置 (页数 - 1) * 需要显示的数据个数-- 查询 第1页 每页显示5条数据
select * from tb_emp limit 0,5;-- 查询 第2页 每页显示5条数据
select * from tb_emp limit 5,5;-- 查询 第3页 每页显示5条数据
select * from tb_emp limit 10,5;