数据库: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语句执行和编写顺序

编写顺序

执行顺序:(标红)

相关推荐
wand codemonkey35 分钟前
SpringbootWeb【入门】+MySQL【安装】+【DataDrip安装 】+【连接MySQL】
java·mysql·mybatis
廿一夏8 小时前
MySql存储引擎与索引
数据库·sql·mysql
lzhdim10 小时前
SQL 入门 15:SQL 事务:从 ACID 到四种常见的并发问题
数据库·sql
瀚高PG实验室11 小时前
瀚高企业版V9.1.1在pg_restore还原备份文件时提示extract函数语法问题
数据库·瀚高数据库
TDengine (老段)11 小时前
TDengine Tag 设计哲学与 Schema 变更机制
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
敲个大西瓜11 小时前
Java项目常用数据归档方式
mysql
YOU OU12 小时前
Spring IoC&DI
java·数据库·spring
Muscleheng13 小时前
Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错
数据库·postgresql
kyriewen13 小时前
面试官让我查各部门工资最高的员工,我用AI三秒写出窗口函数,他愣了
后端·mysql·面试
小码工作室13 小时前
使用 HAVING 进行 MySQL 集合筛选
mysql