数据库:DQL

DQL英文全称是DataQuery Language(数据查询语言,数据查询语言,用来查询数据库中表的记录。

查询关键字:SELECT

语法:

1.基本查询

  1. 查询多个字段

    SELECT 字段1,字段2,字段3 FROM 表名;

    SELECT * FROM 表名;(查询所有字段)

  2. 设置别名

    SELECT 字段1 AS 别名1,字段2 AS 别名2...FROM 表名;(AS可省略)

  3. 去除重复记录

    SELECT DISTINCT 字段列表 FROM 表名;

示例:

sql 复制代码
-- 数据准备
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 '身份证号',
    entrydate   date        comment '入职时间'
)comment '员工表';

insert into emp (id, workno, name, gender, age, idcard, entrydate)
values (1,'101','赵一','男',18,'111111111111111111','2000-2-1'),
       (2,'102','吴二','女',21,'111111111111111112','2000-1-1'),
       (3,'103','张三','男',19,'111111111111111113','2000-1-10'),
       (4,'104','李四','女',22,'111111111111111114','2000-1-15'),
       (5,'105','王五','女',24,'111111111111111115','2000-3-1'),
       (6,'106','老六','男',29,'111111111111111116','2000-6-1');
sql 复制代码
-- 1.基本查询
-- 查询指定字段name,workno,age返回
select name,workno,age from emp;
sql 复制代码
-- 查询所有字段
select * from emp;

sql 复制代码
-- 查询所有员工的年龄,起别名
select age as '年龄' from emp;

2.条件查询(WHERE)

SELECT 字段列表 FROM 表名 WHERE 条件列表;

条件:

示例:

sql 复制代码
-- 2-- 条件查询
-- 查询年龄等于18的员工
select * from emp where age=18;
-- 查询年龄小于20的员工信息
select * from emp where age<20;
-- 查询年龄小于等于21的员工信息
select * from emp where age<=21;

-- 插入一个无身份证号的信息
insert into emp (id, workno, name, gender, age, idcard, entrydate)
values (7,'107','赵四','男',18,NULL,'2000-2-1');
-- 查询没有身份证号的员工信息
select * from emp where idcard is NULL;
-- 查询有身份证号的员工信息
select * from emp where idcard is not NULL;
-- 查询年龄不等于18的员工信息
select * from emp where age!=18;
-- 查询年龄在18岁(包含)到21岁(包含)之间的员工信息
select * from emp where age>=18 && age<=21;
select * from emp where age>=18 and age<=21;
select * from emp where age between 18 and 21;
-- 查询性别为女且年龄小于 21岁的员工信息
select * from emp where gender='女'&&age<=21;
-- 查询年龄等于18或21的员工信息
select * from emp where age=18||age=21;
select * from emp where age=18 or age=21;
-- 插入一个名字为3个字的信息
insert into emp (id, workno, name, gender, age, idcard, entrydate)
    values (8,'108','马云腾','男',18,NULL,'2000-5-1');
-- 查询名字为2个字的员工信息
select * from emp where name like '__';
-- 查询身份证最后一位为5的员工信息
select * from emp where idcard like '%5';
-- 查询姓赵的员工信息
select * from emp where name like '赵%';

3.聚合函数(count、max、min、avg、sum)

聚合函数:将一列数据作为一个整体进行纵向计算。

常见聚合函数:

作用于某一列(某一个属性)。

语法:SELECT 聚合函数(字段列表) FROM 表名;

示例:

sql 复制代码
-- 3.聚合函数
-- 统计员工数量
select count(*) as '员工数量' from emp;    -- 总数据量
select count(id)  as '员工数量' from emp;
-- 员工平均年龄
select  avg(age) as '平均年龄' from emp;
-- 最大年龄和最小年龄
select max(age) as '最大年龄' from emp;
select min(age) as '最小年龄' from emp;
-- 添加新属性工作地址
alter table emp add column workaddress varchar(200) comment '工作地址';
update emp set emp.workaddress='湖北' where id=1;
update emp set emp.workaddress='北京' where id=2;
update emp set emp.workaddress='湖北' where id=3;
update emp set emp.workaddress='湖南' where id=4;
update emp set emp.workaddress='山东' where id=5;
update emp set emp.workaddress='陕西' where id=6;
update emp set emp.workaddress='重庆' where id=7;
update emp set emp.workaddress='西藏' where id=8;
-- 统计湖北的员工的年龄之和
select sum(age) from emp where emp.workaddress='湖北';

4.分组查询(GROUPBY)

语法:SELECT 字段列表 FROM 表名WHERE条件 GROUP BY 分组字段名 HAVING分组后过滤条件;

where与having区别

  • 执时机不同:where是分组之前进过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。
  • 判断条件不同:where不能对聚合函数进行判断,而having可以。

示例:

sql 复制代码
-- 4.分组查询
-- 根据性别分组,统计男女员工数量
select gender as '性别',count(*) as '人数' from emp group by gender;
-- 根据性别分组,统计男女员工平均年龄
select gender as '性别',avg(age) as '平均年龄' from emp group by gender;
-- 查询年龄小于25的员工并按工作地址分组,获取员工数大于1的工作地址
select workaddress as '工作地址',count(*) as '人数' from emp where age<25 group by emp.workaddress having count(*)>1;

5.排序查询(ORDERBY)分页查询(LIMIT)

语法:SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1,字段2 排序方式2,...;

排序方式:ASC(升序),DESC(降序)

示例:

sql 复制代码
-- 5.排序查询
-- 根据年龄升序查询
select * from emp order by age asc;
-- 根据入职时间降序查询
select * from emp order by entrydate desc;
-- 根据年龄升序查询,年龄相同根据入职时间降序排序
select * from emp order by age asc ,entrydate desc;

6.分页查询

语法:SELECT 字段列表 FROM表名 LIMIT 起始索引,查询记录数;

  • 起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数。
  • 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT。
  • 如果查询的是第一页数据,起始索引可以省略,直接简写为limit 10。

示例:

sql 复制代码
-- 6.分页查询
-- 查询第一页员工数据,每页展示4条记录
select * from emp limit 0,4;
-- 查询第二页员工数据,每页展示4条记录
select * from emp limit 4,4;

DQL语句执行和编写顺序

编写顺序

执行顺序:(标红)

相关推荐
GeW1 小时前
世界目光聚焦中国:底层技术、Linux、数据库的破局逻辑
linux·数据库
PingCAP1 小时前
平凯数据库 × 圣诺云数:让治理数据实时可用、跨域可信流通
数据库·tidb
利威尔xu2 小时前
PostgreSQL 索引失效 4 类:函数运算让查询全表扫描拖垮性能
开发语言·数据库·postgresql·性能优化·索引优化
PingCAP2 小时前
挚文集团(陌陌、探探)从 MySQL 分库分表到 TiDB 实时统一架构的实践
数据库·tidb
Decay__2 小时前
SQLServer 运维自动化平台 —— PowerShell + SQL Agent 作业:部署、监控、巡检、告警、工单闭环一体化
运维·数据库·sql·mysql·架构
杨凯凡2 小时前
Spring Framework 7 原生重试与并发限制实战:@Retryable、@ConcurrencyLimit 能替代 Resilience4j 吗?
数据库·spring·sqlserver
星恒随风2 小时前
MySQL 从零入门(十):MVCC、Undo Log 与 Read View 详解——InnoDB 如何实现事务隔离?
linux·数据库·笔记·sql·mysql
zdr尽职尽责3 小时前
终端工具凭空消失:Codex + GPT-6 的一次完整排障复盘
数据库·gpt·学习·ai
txxzjmzlh4 小时前
Redis 介绍及简单使用
数据库·redis·缓存
进击的荆棘4 小时前
MySQL——数据类型
数据库·mysql