mysql中 join 、group by、having、order by、limit 执行顺序

1 SQL大致的解析流程如下

from .. on.. join.. where .. group by .. having ..select distinct ...order by ..  limit..

2 举例说明:
建表及插入数据语句

  • 学生表
    mysql中 join 、group by、having、order by、limit 执行顺序_第1张图片
  • 成绩表
    mysql中 join 、group by、having、order by、limit 执行顺序_第2张图片

需求一:
查询所有学生的平均成绩

SELECT s.*, CAST(AVG(sc.s_score) AS DECIMAL(10,2)) as avg -- cast:将平均分数保留两位小数
FROM Student s
JOIN Score sc ON sc.s_id = s.s_id
GROUP BY sc.s_id   -- GROUP BY 是要在JOIN连接查询之后的

需求二:
查询学生的平均成绩,且平均成绩大于60分

SELECT s.*, CAST(AVG(sc.s_score) AS DECIMAL(10,2)) as avg -- cast:将平均分数保留两位小数
FROM Student s
JOIN Score sc ON sc.s_id = s.s_id
GROUP BY sc.s_id   -- GROUP BY 是要在 JOIN 连接查询之后的
HAVING avg > 60    -- HAVING 条件放在 GROUP BY 之后

需求三:
查询学生的平均成绩,且平均成绩大于60分,按照成绩从高到低排序

SELECT s.*, CAST(AVG(sc.s_score) AS DECIMAL(10,2)) as avg
FROM Student s
JOIN Score sc ON sc.s_id = s.s_id
GROUP BY sc.s_id    -- GROUP BY 是要在 JOIN 连接查询之后
HAVING avg > 60     -- HAVING 条件放在 GROUP BY 之后
ORDER BY avg desc   -- 排序条件放在 HAVING 之后

需求四:
查询学生的平均成绩,且平均成绩大于60分,按照成绩从高到低排序,并显示前五个

SELECT s.*, CAST(AVG(sc.s_score) AS DECIMAL(10,2)) as avg
FROM Student s
JOIN Score sc ON sc.s_id = s.s_id
GROUP BY sc.s_id     -- GROUP BY 是要在 JOIN 连接查询之后
HAVING avg > 60      -- HAVING 条件放在 GROUP BY 之后
ORDER BY avg desc    -- ORDER BY 排序条件放在 HAVING 之后
LIMIT 0,5			 -- LIMIT 放在 HAVING 之后

附:建表及插入数据语句:

-- 学生表
CREATE TABLE Student(
	s_id VARCHAR(20),
	s_name VARCHAR(20) NOT NULL DEFAULT '',
	s_birth VARCHAR(20) NOT NULL DEFAULT '',
	s_sex VARCHAR(10) NOT NULL DEFAULT '',
	PRIMARY KEY (s_id)
	)
-- 成绩表
CREATE TABLE Score(
	s_id VARCHAR(20),
	c_id VARCHAR(20),
	s_score INT(3),
	PRIMARY KEY (s_id, c_id)
	)

-- 插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');

-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

你可能感兴趣的:(msyql)