mySql数据库学习002-表数据查询操作
表数据查询操作
表数据如下:
id | name | age | gender | class | createdAt | updatedAt |
---|---|---|---|---|---|---|
1 | 张三 | 20 | 男 | 一班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
2 | 李四 | 19 | 女 | 一班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
3 | 王五 | 21 | 女 | 二班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
4 | 赵六 | 18 | 女 | 二班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
5 | 孙七 | 19 | 男 | 三班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
6 | 周八 | 19 | 男 | 三班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
7 | 张三丰 | 15 | 男 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
一、基础查询
select id, name from student
查询结果:
id | name |
---|---|
1 | 张三 |
2 | 李四 |
3 | 王五 |
4 | 赵六 |
5 | 孙七 |
6 | 周八 |
7 | 张三丰 |
注意:
- 关键词
select
后id
,name
为要查询的字段,使用*
表示查询所有字段;
二、where条件查询
select * from student where gender = '男'
查询结果:
id | name | age | gender | class | createdAt | updatedAt |
---|---|---|---|---|---|---|
1 | 张三 | 20 | 男 | 一班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
5 | 孙七 | 19 | 男 | 三班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
6 | 周八 | 19 | 男 | 三班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
7 | 张三丰 | 15 | 男 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
解释:
where
后表示查询条件;- 另外,
where
语句支持>
、>=
、<
、<=
、=
、!=
、and
、or
、not
、between ... and ...
、is null
、is not null
、in
、like
、
select * from student where gender = '女' and age > 20
select * from student where gender = '女' or age < 19
select * from student where age > 15 and age < 20
select * from student where age between 16 and 19
select * from student where class is null
select * from student where class is not null
select * from student where age in(18,19)
select * from student where name like '李%'
select * from student where name like '%六'
select * from student where name like '%三%'
三、排序
select * from student where gender = '男' order by age desc
查询结果:
id | name | age | gender | class | createdAt | updatedAt |
---|---|---|---|---|---|---|
1 | 张三 | 20 | 男 | 一班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
5 | 孙七 | 19 | 男 | 三班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
6 | 周八 | 19 | 男 | 三班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
7 | 张三丰 | 15 | 男 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
解释:
order by
为排序的关键词,其中age
为排序的字段名,desc
表示倒序排列,asc
表示正序排列;
四、分页查询
select * from student limit 3 offset 0
select * from student limit 0, 3
查询结果:
id | name | age | gender | class | createdAt | updatedAt |
---|---|---|---|---|---|---|
1 | 张三 | 20 | 男 | 一班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
2 | 李四 | 19 | 女 | 一班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
3 | 王五 | 21 | 女 | 二班 | 2024-04-08 09:15:09 | 2024-04-08 09:15:09 |
解释:
limit
表示分页大小,offset
表示偏移量;- 在不使用
offset
关键词时,limit
后第一个参数为偏移量,第二个参数为分页大小; 偏移量
通常需要根据前端传值的pageSize
,pageNum
计算得来;
五、聚合函数
select sum(age) sum_age from student
select avg(age) avg_age from student
select count(*) stu_count from student
select max(age) max_age from student
select min(age) min_age from student
查询结果:
sum_age |
---|
131 |
解释:
- 聚合函数需要传参,参数可以为
*
,或者数据表中的字段; - 另外,对于聚合函数的结果,可以设置
别名
,如sum_age
表示查询到数据的年龄和;
六、分组查询
select class, count(class) class_count from student group by class order by class_count desc
查询结果:
class | class_count |
---|---|
三班 | 2 |
一班 | 2 |
二班 | 2 |
Null | 0 |
解释:
- 其中
class
为分组字段,group by
为分组查询关键词,class_count
为别名; - 在对数据进行分组的时候,
select
后面必须是分组字段或者聚合函数;
七、having条件查询
select class, avg(age) avgAge from student group by class having avgAge > 19
查询结果:
class | avgAge |
---|---|
一班 | 19.5 |
二班 | 19.5 |
解释:
class
为分组字段,avg(age)
聚合函数求年龄平均值,group by
分组关键词,having
分组关键词,表示从返回的结果集中筛选平均年龄大于等于19的班级;- 注意:最好设置聚合函数结果的别名
avgAge
;
八、Q & A
- 什么是结果集?有什么特点?与表有什么区别与联系?
- 通过查询语句从数据表中查询出来的结果成为结果集;以表的形式呈现;结果集和查询的表不是同一张表,结果集来自数据表;结果集保存在内存中,而数据表保存在硬盘上;
- 什么是聚合函数?怎么使用聚合函数?
- 对表中的数据进行统计和计算,一般结合分组(GROUP BY)来使用,用于统计和计算分组数据
- COUNT() 计算查询数据的数据总量
- SUM() 计算查询结果中所有指定字段的和
- AVG() 计算查询结果中所有指定字段的平均值
- MAX() 求查询结果中指定字段的最大值
- MIN() 求查询结果中指定字段的最小值;
- having 与 where 的区别是什么?
where
是去数据表中查询符合条件的数据,返回结果集;having
是去数据集中查询符合条件的数据,可以对分组之后查询到的结果进行筛选;