是用来查询数据库表的记录的语句
在SQL语句中占有90%以上
也是最为复杂的操作 最为繁琐的操作
DQL语句很重要很重要
初始化数据库和表
USE dduo; create table tb_emp( id int unsigned primary key auto_increment comment 'ID', username varchar(20) not null unique comment '用户名', password varchar(36) 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 学工主管 ', entrydata date comment '入职位时间', create_time datetime not null comment '创建时间', update_time datetime not null comment '修改时间' )comment '员工表'; INSERT INTO tb_emp VALUES (1,'gaochang','123456','高畅',2,'1.jpg',1,'2000-01-01','2022-10-27 17:12:32','2022-10-27 17:12:32'), (2,'luanzengxv','123456','栾增旭',1,'2.jpg',2,'2000-01-01','2022-10-27 17:12:32','2022-10-27 17:12:32'), (3,'liuyan','123456','刘岩',1,'3.jpg',3,'2000-01-01','2022-10-27 17:12:32','2022-10-27 17:12:32');
基本语法
USE dduo; -- 查询指定字段name entrydate并返回 select name ,entrydata from tb_emp; -- 查询所有字段 #不推荐的方式 不直观 性能低 建议一个个的输入 select * from tb_emp; -- 查询name 并起别名(姓名) -- 字段展示时会自动变化 select name as 姓名 from tb_emp; select name 姓名 from tb_emp; select name '姓名' from tb_emp; select name '姓 名' from tb_emp; -- 查询员工一共有多少种密码 不能重复 select distinct tb_emp.password from tb_emp;
注意事项
*号表示查询所有字段 在实际开发中尽量少用 不直观而且影响效率
条件查询
在基本查询的基础上加上条件
我们主要学习的是条件的构建方式
USE dduo; -- 查询姓名为高畅的员工 SELECT * from user where name='高畅'; -- 查询age小于等于20的员工 SELECT * from user where age<20; -- 查询age是null的员工信息 SELECT * from user where age is null; SELECT * from user where age is not null; -- 查询age不等于19的信息 SELECT * from user where age!=19; -- 查询指定创建日期的员工信息 SELECT * from user where creat_time >='2024-01-01 ' and creat_time<='2024-12-12' ; SELECT * from user where creat_time between '2024-01-01 ' and'2024-12-12' ; -- 查询指定创建日期并且年龄为20的员工信息 SELECT *from user where creat_time between '2024-01-01' and '2024-12-12' && age =20 ; SELECT *from user where creat_time between '2024-01-01' and '2024-12-12' and age =20 and name='高畅'; -- 查询年龄是19或者20的员工信息 SELECT *from user where age=19 || age=20; SELECT *from user where age=19 or age=20; SELECT *from user where age in (19,20); -- 查询姓名为两个字符的员工信息 (模糊查询) SELECT *FROM user WHERE name LIKE '__'; SELECT *FROM user WHERE name LIKE '___'; -- 查询姓氏为高的员工 SELECT *FROM user WHERE name LIKE '高%' or '高%%';
注意事项
null 和 模糊查询的两个占位符
聚合函数
为分组查询打下基础
将一列数据作为一个整体 进行纵向运算
use dduo; -- 聚合函数 -- 统计该企业的员工数量 (非空字段) -- 统计数据库中所有的数据量 建议使用count(*) select count(name) from user; select count('1') from user; select count(*) from user; -- 统计最早更新日期的员工 select min(user.update_time) from user; select max(user.update_time) from user; -- 统计更新日期的平均值 select avg(user.update_time) from user; -- 求年龄之和 select sum(user.age) from user;
注意事项
null值不参与所有的聚合函数的运算
统计数量可以使用:count(*) count(字段) count(常量)
推荐使用 count( * )
分组查询
use dduo; -- 根据年龄分组 统计各年龄的员工数量 select user.age,count(*) from user group by age ; -- 先查询更新时间 再根据年龄筛选 数量大于3的年龄 select age,count(*) from user where update_time< '2025-01-01' group by age having count(*)>2;
面试题
注意事项
排列查询
use dduo; -- 根据更新时间 对员工进行升序排序 select *from user order by update_time asc; -- 降序排序 select *from user order by update_time desc; -- 根据创建时间对员工进行升序排列 如果相同 按照更新时间进行降序排序 select *from user order by creat_time asc ,update_time desc ;
注意事项
如果是多字段排序 当第一个字段值相同时 才会根据第二个字段进行排序
分页查询
use dduo; -- 分页查询 -- 1.从起始索引0开始 开始查询员工数据 每页展示1条记录 select *from user limit 0,1; -- 查询第1页 员工数据每页展示2条记录 select *from user limit 2,2; -- 查询第2页 员工数据每页展示2条记录 select *from user limit 4,2; -- 查询第3页 员工数据每页展示2条记录 select *from user limit 6,2; -- 查询第4页 员工数据每页展示1条记录 select *from user limit 8,1;
起始索引 = (页码-1) * 每页展示的记录数
将来开发关系型数据库的时候
前端并不会把起始索引传递过来 而是传递页码
我们要换算成索引 在MySQL中书写SQL语句