数据库:SQL分类之DQL详解

1.DQL语法

select

字段列表

from

表名列表

where

条件列表

group by

分组字段列表

having

分组后条件列表

order by

排序字段列表

limit

分页参数
基本查询

条件查询(where)

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

分组查询(group by)

排序查询(order by)

分页查询 (limit)

2.基础查询

1.查询指定字段

select 字段1,字段2,字段3 from 表名;

例:

select name,workno,age from employee;
2.查询所有字段返回

select 字段1,字段2,字段3 from 表名;

select * from 表名;

例:

select id,workno,name,gender,age,idcard,workaddress,entrydate from employee;

select * from employee;
3.设置别名

select 字段 as 别名 from 表名;

例:

select workaddress as '工作地址' from employee;

select workaddress as  from employee;
4.去除重复记录

select distinct 字段列表 from 表名;

例:

select distinct workaddress '工作地址' from employee;


select distinct workaddress from employee;

3.条件查询

1.语法

select 字段列表 from 表名 where 条件列表;

2.条件

|--------------------|-----------------------|
| 比较运算符 | 功能 |
| > | 大于 |
| >= | 大于等于 |
| < | 小于 |
| <= | 小于等于 |
| = | 等于 |
| <> 或 != | 不等于 |
| between ... and .. | 在某个范围之内(含最小、最大值) |
| in(...) | 在 in 之后的列表中的值,多选一 |
| like 占位符 | 模糊匹配(_匹配单个字符,%匹配任意字符) |
| is NULL | 是 NULL |

|------------|----------------|
| 逻辑运算符 | 功能 |
| and 或 && | 并且(多个条件同时成立) |
| OR 或 || | 或者(多个条件任意一个成立) |
| NOT 或 ! | 非、不是 |

例:

select *  from employee where age = 88;

select * from employee where age <30;

select * from employee where idcard is NULL;

select * from employee where idcard is not null;

select * from employee where age != 88;

select *from employee  where age in(19,20,21);

//查找名字是两个字的员工
select *from employee where name like '__';//注意这里面是两个下划线

//查找身份证最后一位是X的员工
select *from employee where idcard like '%X';
select *from employee where idcard like '_________________X';// 17个下划线

4.聚合函数

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

|-------|------|
| 函数 | 功能 |
| count | 统计数量 |
| max | 最大值 |
| min | 最小值 |
| sum | 求和 |
| avg | 平均值 |
[常见的聚合函数]

2.语法:

select 聚合函数 (字段列表) from 表名;

例:

select count(*) from employee;

select count(id) from employee;

select * from employee where workaddress = '西安';

select sum(age) from employee where workaddress = '西安';

注意:NULL 值不参与所有聚合函数运算。

5.分组查询

1.语法

select 字段列表 from 表名 (where 条件)group by 分组字段名 (having 分组后过滤条件)

注意:

  • 执行顺序:where > 聚合函数 > having
  • 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。
2.where 和 having 的区别
  1. 执行时间不同:where 是分组之前进行过滤,不满足 where 条件,不参与分组;而 having 是分组之后对结果进行过滤。
  2. 判断条件不同:where 不能对聚合函数进行判断,而 having 可以。

例:

//查询性别分组,统计男性员工 和 女性员工 的数量
select gender,count(*) from employee group by gender;

//根据性别分组,统计男性员工 和 女性员工 的平均年龄
select gender,avg(age) from employee group by gender;

//查询员工年龄小于30,并根据工作地址分组
select workaddress,count(*) from employee where age < 30 group by workaddress ;

//查询员工年龄小于30,并根据工作地址分组,获取员工数量大于等于2的工作地址

select workaddress,count(*) from employee where age < 30 group by workaddress having count(*) >= 2;

6.排序查询

1.语法

select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;

例:

//年龄升序排序
select * from employee order by age asc;

//年龄进行倒序
select * from employee order by age desc;

//先按照年龄升序排序,再根据入职时间降序
select * from employee order by age asc, entrydate desc;

7.分页查询

1.语法

select 字段列表 from 表名 limit 起始索引,查询记录数;

注意 :

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

例:

//查询第一页员工数据,每页展示10条记录
select * from employee limit 0,10;

//查询第二页员工数据,每页展示10条数据
select *from employee limit 10,10;

8.执行顺序

9.课后训练

为了对大家的知识进行巩固,我准备了下面一些例题,大家可以试试看。稍后我会把答案放置评论区。

1.查询年龄为20,21,24的女性员工信息

2.查询性别为男,并且年龄在25-29(含)岁以内的姓名为三个字的员工

3.年龄小于60岁,男性员工和女性员工的人数

4.查询年龄小于等于35岁的员工姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同,按入职时间降序排序

5.性别为男,且年龄在23-25之间的前5个员工的信息,并对查询结果按年龄升序排序,如果年龄相同,按入职时间降序排序

本节知识讲解就到此结束啦,下期再见!

相关推荐
YGGP12 分钟前
【每日八股】Redis篇(二):数据结构
数据结构·数据库·redis
愉悦的麻婆豆腐23 分钟前
Neo4j使用neo4j-admin导入csv数据方法
数据库·neo4j
夜泉_ly3 小时前
MySQL -安装与初识
数据库·mysql
qq_529835354 小时前
对计算机中缓存的理解和使用Redis作为缓存
数据库·redis·缓存
月光水岸New7 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6757 小时前
数据库基础1
数据库
我爱松子鱼7 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo7 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser8 小时前
【SQL】多表查询案例
数据库·sql
Galeoto8 小时前
how to export a table in sqlite, and import into another
数据库·sqlite