一、DQL 的定义
DQL(Data Query Language,数据查询语言)是 SQL 语言的一个子集,用于从数据库中查询数据。DQL 的核心是 SELECT
语句,通过指定查询条件、排序、分组等操作,用户可以高效地从数据库中提取所需数据。
二、DQL 的语法结构
基本语法:
select 列名1, 列名2, ...
from 表名
[where 条件]
[group by 列名]
[having 条件]
[order by 列名 asc|desc]
[limit 分页参数];
各部分的功能
select
:指定要查询的列。from
:指定查询数据的表。where
:设置查询条件,过滤数据。group by
:对查询结果进行分组。having
:对分组后的数据进行过滤。order by
:对查询结果进行排序。limit
:限制返回的行数,常用于分页查询。
基本查询:(select...from...):
-- 查询多个字段
select 字段1, 字段2, 字段3
from 表名;
-- 查询所有字段(通配符)
select *
from 表名;
-- 为查询字段设置别名,as 关键字可以省略
select 字段1 as 别名1,
字段2 as 别名2
from 表名;
-- 去除重复记录
select distinct 字段列表
from 表名;
语法示例:
-- 1. 查询指定字段 name,entry_date 并返回
select name,entry_date from emp;
-- 2. 查询返回所有字段
-- 方式一 (推荐, 直观/性能高)
select id, username, password, name, gender, phone, job, salary, image, entry_date, create_time, update_time from emp;
-- 方式二 (学习)
select * from emp;
-- 3. 查询所有员工的 name,entry_date, 并起别名(姓名、入职日期)
select name as '姓名' , entry_date as '入职日期' from emp;
select name '姓名' , entry_date '入职日期' from emp;
-- 4. 查询已有的员工关联了哪几种职位(不要重复) - distinct
select distinct job from emp;
条件查询(where):
select 字段列表 from 表名 where 条件列表;
语法示例:
-- 1. 查询 姓名 为 柴进 的员工
select * from emp where name = '柴进';
-- 2. 查询 薪资小于等于5000 的员工信息
select * from emp where salary <= 5000;
-- 3. 查询 没有分配职位 的员工信息
select * from emp where job is null;
-- 4. 查询 有职位 的员工信息
select * from emp where job is not null ;
-- 5. 查询 密码不等于 '123456' 的员工信息
select * from emp where password != '123456';
select * from emp where password <> '123456';
-- 6. 查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息
select * from emp where entry_date between '2000-01-01' and '2010-01-01';
-- 7. 查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息
select * from emp where (entry_date between '2000-01-01' and '2010-01-01') and gender = 2;
-- 8. 查询 职位是 2 (讲师), 3 (学工主管) 的员工信息
-- 方式一
select * from emp where job = 2 or job = 3;
-- 方式二
select * from emp where job in (2,3);
-- 9. 查询 姓名 为两个字的员工信息
select * from emp where name like '__';
-- 10. 查询 姓 '李' 的员工信息
select * from emp where name like '李%';
-- 11. 查询 姓名中包含 '二' 的员工信息
select * from emp where name like '%二%';
分组查询(group by):
select 字段列表 from 表名 [where 条件列表] group by 分组字段名 [having 分组后过滤条件];
where 与 having 的区别
-
执行时机不同
where
是在分组之前进行过滤,不满足where
条件的数据将不参与分组。having
是在分组之后对分组结果进行过滤。
-
判断条件不同
where
不能对聚合函数进行判断。having
可以对聚合函数进行判断。
语法示例:
-- 聚合函数
-- 1. 统计该企业员工数量 - count : count(*) ~ count(常量) > count(字段)
-- count(*)
select count(*) from emp;
-- count(字段)
select count(id) from emp;
-- count(常量)
select count(0) from emp;
-- 2. 统计该企业员工的平均薪资 -- avg
select avg(salary) from emp;
-- 3. 统计该企业员工的最低薪资 -- min
select min(salary) from emp;
-- 4. 统计该企业员工的最高薪资 -- max
select max(salary) from emp;
-- 5. 统计该企业每月要给员工发放的薪资总额(薪资之和) -- sum
select sum(salary) from emp;
含有having的情况:
/*先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 ,
获取员工数量大于等于2的职位*/
select job, count(*) from emp where entry_date <= '2015-01-01' group by job
having count(*) >= 2;
排序查询(order by):
排序方式:升序(asc),降序(desc);默认为升序asc,是可以不写的。
-- 排序查询
select 字段列表
from 表名
[where 条件列表]
[group by 分组字段名
having 分组后过滤条件]
order by 排序字段 排序方式;
注意:在 SQL 中,ORDER BY
子句支持多字段排序,能够根据多列的优先级依次排序。当第一个字段的值相同,会根据第二个字段排序,依次类推。
语法示例:
/*优先按 department_id 升序排序(同一个部门的员工排在一起)
当 department_id 相同时,再按 age 降序排序(年龄大的排在前面)*/
select name, department_id, age
from employees
order by department_id asc, age desc;
分页查询(limit):
-- 排序查询
select 字段
from 表名
[where 条件]
[group by 分组字段
having 过滤条件]
[order by 排序字段]
limit 起始索引, 查询记录数;
1.起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数。
2.分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT。
3.如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 查询记录数。
三、总结
DQL 是 SQL 语言中最常用的部分,其核心是 SELECT
语句,涉及语法较多,值得细细品味。