大家好,在之前的文章中,我们学习了数据库的创建、表的操作以及增删改查的基础用法。今天我们将深入 mysql 查询 的核心,系统性地讲解 排序、分页、分组统计、聚合函数 以及 多表关联查询。这些技能是数据分析、报表生成和后端接口开发的基础。
一、数据准备
为了方便演示,我们先创建一张 students 表和一张 scores 表:
create database if not exists school;
use school;
-- 学生表
create table students (
id int primary key auto_increment,
name varchar(50) not null,
age int,
class varchar(20),
gender enum('男', '女'),
created_at datetime default current_timestamp
);
-- 成绩表
create table scores (
id int primary key auto_increment,
student_id int,
subject varchar(50),
score decimal(5,2),
exam_date date,
foreign key (student_id) references students(id)
);
-- 插入测试数据
insert into students (name, age, class, gender) values
('张三', 20, '计算机1班', '男'),
('李四', 21, '计算机1班', '女'),
('王五', 19, '计算机2班', '男'),
('赵六', 22, '计算机2班', '女'),
('孙七', 20, '计算机1班', '男'),
('周八', 21, '计算机2班', '女'),
('吴九', 19, '计算机3班', '男'),
('郑十', 22, '计算机3班', '女');
insert into scores (student_id, subject, score, exam_date) values
(1, '数学', 85, '2025-01-10'),
(1, '英语', 78, '2025-01-10'),
(2, '数学', 92, '2025-01-10'),
(2, '英语', 88, '2025-01-10'),
(3, '数学', 76, '2025-01-10'),
(3, '英语', 82, '2025-01-10'),
(4, '数学', 95, '2025-01-10'),
(4, '英语', 90, '2025-01-10'),
(5, '数学', 68, '2025-01-10'),
(5, '英语', 75, '2025-01-10');二、排序:order by
order by 用于对查询结果进行排序,默认为升序(asc),可指定降序(desc)。
2.1 单列排序
-- 按年龄升序排列(默认) select * from students order by age; -- 按年龄降序排列 select * from students order by age desc; -- 按姓名排序(字符串按字母/拼音顺序) select * from students order by name;
2.2 多列排序
-- 先按班级升序,再按年龄降序 select * from students order by class asc, age desc; -- 先按年龄降序,再按姓名升序 select * from students order by age desc, name asc;
三、分页:limit
limit 用于限制查询返回的行数,是实现分页功能的关键。
3.1 基础用法
-- 返回前 3 条记录 select * from students limit 3; -- 返回第 3 条之后的 3 条记录 select * from students limit 3 offset 3; -- 等价写法 select * from students limit 3, 3; -- 注意:第一个数字是偏移量,第二个是行数
3.2 分页公式
分页的核心公式:
-- 第 page 页,每页 size 条 select * from 表名 limit (page - 1) * size, size;
-- 第 1 页(每页 3 条) select * from students limit 0, 3; -- 第 2 页(每页 3 条) select * from students limit 3, 3; -- 第 3 页(每页 3 条) select * from students limit 6, 3;
3.3 分页与排序结合
-- 先按年龄降序排序,再取第 2 页(每页 3 条) select * from students order by age desc limit 3, 3;
3.4 获取总记录数
分页时通常需要同时获取总记录数,用于前端显示总页数。
-- 查询总条数 select count(*) from students; -- 结合分页查询 select * from students order by age desc limit 0, 3; -- 再执行:select count(*) from students;
四、聚合函数
聚合函数对一组值执行计算,返回单个结果值。
4.1 常用聚合函数
| 函数 | 说明 |
|---|---|
count() | 统计行数 |
sum() | 计算总和 |
avg() | 计算平均值 |
max() | 返回最大值 |
min() | 返回最小值 |
-- 统计学生总数
select count(*) as total_students from students;
-- 统计年龄总和
select sum(age) as total_age from students;
-- 计算平均年龄
select avg(age) as avg_age from students;
-- 统计男生人数
select count(*) from students where gender = '男';
-- 分数统计
select
count(*) as 考试人数,
avg(score) as 平均分,
max(score) as 最高分,
min(score) as 最低分,
sum(score) as 总分
from scores;五、分组:group by
group by 将数据按指定列分组,通常与聚合函数配合使用。
5.1 基础用法
-- 统计每个班级的学生人数
select
class,
count(*) as 人数
from students
group by class;
-- 统计每个性别的学生人数
select
gender,
count(*) as 人数
from students
group by gender;5.2 分组聚合统计
-- 按班级统计最大年龄和最小年龄
select
class,
max(age) as 最大年龄,
min(age) as 最小年龄
from students
group by class;
-- 按学生统计各科平均分
select
student_id,
avg(score) as 平均分,
count(*) as 考试科目数
from scores
group by student_id;5.3 分组后筛选:having
having 用于对分组后的结果进行筛选,与 where 的区别是:
where在分组前筛选原始数据having在分组后筛选聚合结果
-- 筛选出学生人数大于 2 的班级
select
class,
count(*) as 人数
from students
group by class
having count(*) > 2;
-- 筛选出平均分大于 80 的学生
select
student_id,
avg(score) as 平均分
from scores
group by student_id
having avg(score) > 80;5.4 where 与 having 的对比
-- where:过滤原始数据(在分组前执行)
select
class,
count(*) as 人数
from students
where age >= 20 -- 先筛选年龄 >= 20 的学生
group by class; -- 再分组统计
-- having:过滤分组后的结果(在分组后执行)
select
class,
count(*) as 人数
from students
group by class
having count(*) >= 3; -- 只显示人数 >= 3 的班级六、常用查询模板
-- 完整的查询语句顺序(语法顺序) select 列名 from 表名 join 其他表 on 条件 where 行级过滤 group by 分组列 having 组级过滤 order by 排序列 limit 偏移量, 行数;
执行顺序(逻辑顺序):
from → join → where → group by → having → select → order by → limit
七、总结
| 知识点 | 核心语法 |
|---|---|
| 排序 | order by 列名 asc / desc |
| 分页 | limit offset, size |
| 聚合函数 | count()、sum()、avg()、max()、min() |
| 分组 | group by 列名 |
| 分组筛选 | having 条件(区别于 where) |
| 执行顺序 | from → where → group by → having → select → order by → limit |
排序、分页、分组和聚合是数据查询中最常用的功能组合。在实际开发中,这些操作往往需要配合使用 —— 先关联表获取完整数据,再用 where 过滤、group by 分组、having 筛选、order by 排序,最后用 limit 分页返回结果。掌握这套组合拳,就能应对绝大多数数据查询需求。
到此这篇关于mysql中数据查询完全指南:分页、排序、分组、聚合与高级查询的文章就介绍到这了,更多相关mysql数据库查询内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论