Python学习之复习MySQL-Day3(DQL)
2023-12-16 21:38:44
目录
文章声明???
- 该文章为我(有编程语言基础,非编程小白)的 MySQL复习笔记
- 知识来源为 B站UP主(黑马程序员)的MySQL课程视频,归纳为自己的语言与理解记录于此并加以实践
- 此前我已经学习过了MySQL,现在是在复习阶段,所以不是面向小白的教学文章
- 不出意外的话,我大抵会 持续更新
- 想要了解前端开发(技术栈大致有:Vue2/3、微信小程序、uniapp、HarmonyOS、NodeJS、Typescript)与Python的小伙伴,可以关注我!谢谢大家!
让我们开始今天的学习吧!
DQL简介
DQL英文全称Data Query Language,即数据查询语言,用来查询表中数据
完整语法如下:
select 字段列表 from 表名列表 where 条件列表 group by 分组字段列表 having 分组后条件列表 order by 排序字段列表 limit 分页参数
基本查询
查询多个/全部字段
mysql> select id,username from emp;
+------+----------+
| id | username |
+------+----------+
| 1 | Richie |
| 2 | Taylor |
| 3 | Mike |
+------+----------+
3 rows in set (0.00 sec)
mysql> select * from emp; # 数据量大的时候尽量不要使用,因为效率比较低
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 3 | Mike | 45 | 男 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
设置别名
设置别名的好处在于,我们可以自定义字段名,可以更直观的观察数据
mysql> select username as name,gender as sex from emp;
+--------+------+
| name | sex |
+--------+------+
| Richie | 男 |
| Taylor | 女 |
| Mike | 男 |
+--------+------+
3 rows in set (0.00 sec)
mysql> select username name,gender sex from emp; # as可以省略
+--------+------+
| name | sex |
+--------+------+
| Richie | 男 |
| Taylor | 女 |
| Mike | 男 |
+--------+------+
3 rows in set (0.00 sec)
去除重复记录
mysql> select distinct gender '性别' from emp; # 使用关键字distinc来去除重复记录
+------+
| 性别 |
+------+
| 男 |
| 女 |
+------+
2 rows in set (0.00 sec)
条件查询
条件查询介绍
比较运算符 | 功能 |
---|---|
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
= | 等于 |
<> 或 != | 不等于 |
between … and … | 在某个范围之内(含最大最小值) |
in(…) | 在in之后的列表中的值,就是多选一的意思 |
like 占位符 | 模糊匹配(_匹配单个字符,%匹配任意个字符) |
is null | 是null |
逻辑运算符 | 功能 |
---|---|
and 或 && | 并且(多个条件同时成立) |
or 或 || | 或者(多个条件任意一个成立) |
not 或 ! | 非(不是) |
实例演示
mysql> select * from emp where age = 88; # 查询年龄等于88的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 5 | 张三 | 88 | |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where age < 30; # 查询年龄小于30的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from emp where age <= 25; # 查询年龄小于等于25的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from emp where username = '' or username is null; # 查询没有姓名的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 4 | | 25 | 女 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where not (username = '' or username is null); # 查询有姓名的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 3 | Mike | 45 | 男 |
| 5 | 张三 | 88 | |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp where age != 45; # 查询年龄不等于45的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 4 | | 25 | 女 |
| 5 | 张三 | 88 | |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp where age between 20 and 40; # 查询年龄在20-40之间的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
mysql> select * from emp where gender = '女' and age < 30; # 查询性别为女且年龄小于30的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 4 | | 25 | 女 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where gender = '女' and age < 30; # 查询年龄为21或者25或者34的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 4 | | 25 | 女 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where age in (21,25,34); # 查询年龄为21或者25或者34的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 4 | | 25 | 女 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
mysql> select * from emp where username like '__'; # 查询姓名为两个字的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 5 | 张三 | 88 | |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> select * from emp where username like '%e'; # 查询姓名最后是e的员工
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 3 | Mike | 45 | 男 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
聚合函数
什么是聚合函数?
聚合函数用于将一列数据作为一个整体,进行纵向的计算
注意:null不参与聚合函数的运算
常见的聚合函数
函数 | 功能 |
---|---|
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
实例演示
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 34 | 女 |
| 3 | Mike | 45 | 男 |
| 4 | | 25 | 女 |
| 5 | 张三 | 88 | |
+------+----------+------+--------+
5 rows in set (0.00 sec)
mysql> select count(id) from emp; # 统计有多少条包含id字段的记录,不统计null
+-----------+
| count(id) |
+-----------+
| 5 |
+-----------+
1 row in set (0.00 sec)
mysql> select avg(age) from emp; # 统计员工的平均年龄
+----------+
| avg(age) |
+----------+
| 42.6000 |
+----------+
1 row in set (0.03 sec)
mysql> select max(age) from emp; # 统计员工的最大年龄
+----------+
| max(age) |
+----------+
| 88 |
+----------+
1 row in set (0.00 sec)
mysql> select min(age) from emp; # 统计员工的最小年龄
+----------+
| min(age) |
+----------+
| 21 |
+----------+
1 row in set (0.00 sec)
mysql> select sum(age) from emp where gender = '男'; # 统计男员工的年龄之和
+----------+
| sum(age) |
+----------+
| 66 |
+----------+
1 row in set (0.00 sec)
分组查询
分组查询语法
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤的条件]
where 和 having 的区别
- 执行时机不同:where 是在 group by 分组之前进行过滤,不满足 where 条件的,不参与 group by 分组;而 having 则是在 group by 分组之后过滤的
- 判断条件不同:where 不能对聚合函数进行判断过滤,而 having 则可以
注意:执行顺序为 where > 聚合函数 > having
实例演示
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
| 5 | Joker | 56 | 男 |
| 6 | Nancy | 18 | 女 |
+------+----------+------+--------+
6 rows in set (0.00 sec)
mysql> select gender,count(*) from emp group by gender; # 根据性别分组,统计男员工和女员工的数量
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 4 |
| 女 | 2 |
+--------+----------+
2 rows in set (0.00 sec)
mysql> select gender,avg(age) from emp group by gender; # 根据性别分组,统计男员工和女员工的平均年龄
+--------+----------+
| gender | avg(age) |
+--------+----------+
| 男 | 50.7500 |
| 女 | 23.5000 |
+--------+----------+
2 rows in set (0.03 sec)
mysql> select gender,count(*) from emp where age < 60 group by gender having count(*) >= 3; # 查询年龄小于60的员工,并根据性别分组,获取员工数量大于等于3的性别
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男 | 3 |
+--------+----------+
1 row in set (0.00 sec)
mysql> select gender,count(*) num from emp where age < 60 group by gender having num >= 3; # 还可以在上一个例题中起别名
+--------+-----+
| gender | num |
+--------+-----+
| 男 | 3 |
+--------+-----+
1 row in set (0.03 sec)
排序查询
语法
select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2 ... ;
排序方式
- 升序:asc(ascend,此为默认值)
- 降序:desc(descend)
实例演示
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
| 5 | Joker | 56 | 男 |
| 6 | Nancy | 18 | 女 |
+------+----------+------+--------+
6 rows in set (0.00 sec)
mysql> select username '姓名',age '年龄',gender '性别' from emp where gender = '男' order by age; # 根据 年龄对男员工进行升序排序
+--------+------+------+
| 姓名 | 年龄 | 性别 |
+--------+------+------+
| Richie | 21 | 男 |
| Mike | 38 | 男 |
| Joker | 56 | 男 |
| Jack | 88 | 男 |
+--------+------+------+
4 rows in set (0.00 sec)
分页查询
语法
select 字段列表 from 表名 limit 起始索引 , 查询记录数;
注意:
- 起始索引从 0 开始计算,起始索引 = (要查询的页码数 - 1)* 每页显示记录数
- 如果查询的是第一页,起始索引可以省略
实例演示
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
| 5 | Joker | 56 | 男 |
| 6 | Nancy | 18 | 女 |
| 7 | Linken | 57 | 男 |
+------+----------+------+--------+
7 rows in set (0.00 sec)
mysql> select * from emp limit 0 , 4; # 查询第一页员工数据,每页展示4条数据
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp limit 4; # 查询第一页数据时可以简写
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Taylor | 29 | 女 |
| 3 | Mike | 38 | 男 |
| 4 | Jack | 88 | 男 |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> select * from emp limit 5,5; # 查询第二页数据,每页展示5条数据
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 6 | Nancy | 18 | 女 |
| 7 | Linken | 57 | 男 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
综合练习
# 查询年龄为20,21,22,23岁的女性员工信息
select * from emp where age in (20,21,22,23) && gender = '女';
# 查询性别为男,并且年龄在 20-40 (不包含20岁包含40岁)岁且姓名长度等于4的员工信息
select * from emp where gender = '男' && age between 21 and 40 && name like '____';
# 统计员工表中年龄小于60岁的男性员工和女性员工的人数
select gender , count(*) from emp where age < 60 group by gender
# 查询所有年龄小于等于35岁的员工姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序
select name,age from emp where age <= 35 order by age asc,entrytime desc;
# 查询性别为男,并且年龄在 20-40岁(包含20、40岁)以内的前五名员工信息
select * from emp where gender = '男' && age between 20 and 40 limit 5;
文章来源:https://blog.csdn.net/Richieeea/article/details/134995153
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!