![91e2cac241b0e7738dc45b71736cd398.png](https://i-blog.csdnimg.cn/blog_migrate/c82c258d674097f8ea90c9e881e059c5.jpeg)
1.创建总表score;
mysql> use test;
Database changed
mysql> CREATE TABLE score (
-> id int(10) NOT NULL AUTO_INCREMENT,
-> subject_id int(10) DEFAULT NULL,
-> student_id int(10) DEFAULT NULL,
-> score float DEFAULT NULL,
-> PRIMARY KEY (id)
-> ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 4 warnings (0.61 sec)
![93198ef8f7f68be5dd67c1a10ec21324.png](https://i-blog.csdnimg.cn/blog_migrate/bed9722979f5d581b7ae1c826d63a331.png)
2.输入表中内容;
mysql> INSERT INTO SCORE VALUES(1,1,1,96),(2,1,2,86),(3,1,3,99),(4,1,4,78),(5,1,5,89);
Query OK, 5 rows affected (0.43 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> INSERT INTO SCORE VALUES(6,2,1,97),(7,2,2,88),(8,2,3,98),(9,2,4,79),(10,2,5,87);
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> INSERT INTO SCORE VALUES(11,3,1,99),(12,3,2,87),(13,3,3,97),(14,3,4,77),(15,3,5,88);
Query OK, 5 rows affected (0.12 sec)
Records: 5 Duplicates: 0 Warnings: 0
![e83007e7363b0524203c44812b1d6c65.png](https://i-blog.csdnimg.cn/blog_migrate/b92ceeacfad62826d9bd130a1785f60a.png)
3.输入具体的表内容;
mysql> CREATE TABLE student (
-> id int(10) NOT NULL AUTO_INCREMENT,
-> name varchar(10) DEFAULT NULL,
-> PRIMARY KEY (id)
-> ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 2 warnings (0.30 sec)
![3b11931e288781dd58250063109aee20.png](https://i-blog.csdnimg.cn/blog_migrate/cdda8f1c332fc7591cc544b4bf2e2e24.png)
INSERT INTO STUDENT VALUES(1,'张斌'),(2,'李四'),(3,'王五'),(4,'周六'),(5,'张三');
Query OK, 5 rows affected (0.72 sec)
Records: 5 Duplicates: 0 Warnings: 0
![11700da7d0737a34957b21a113ce8a37.png](https://i-blog.csdnimg.cn/blog_migrate/ba8267e6a8307eb5ccc11784b797c06d.png)
INSERT INTO SUBJECT VALUES(1,'java'),(2,'c++'),(3,'python');
Query OK, 3 rows affected (0.53 sec)
Records: 3 Duplicates: 0 Warnings: 0
![2e399f4b440ceb31ba9b9984b979fb39.png](https://i-blog.csdnimg.cn/blog_migrate/93683813fab32a4f79240e7d6b0559ae.png)
4.查询
select a.id,a.subject_id,a.student_id,a.score from score as a left join score as b on a.subject_id=b.subject_id and a.score>=b.score
group by a.subject_id,a.student_id,a.score
having count(a.subject_id)>=3
order by a.subject_id,a.score desc;
+----+------------+------------+-------+
| id | subject_id | student_id | score |
+----+------------+------------+-------+
| 3 | 1 | 3 | 99 |
| 1 | 1 | 1 | 96 |
| 5 | 1 | 5 | 89 |
| 8 | 2 | 3 | 98 |
| 6 | 2 | 1 | 97 |
| 7 | 2 | 2 | 88 |
| 11 | 3 | 1 | 99 |
| 13 | 3 | 3 | 97 |
| 15 | 3 | 5 | 88 |
+----+------------+------------+-------+
9 rows in set (0.00 sec) #这里把所有的列都列出来了便于对比 这里把表score的每一条同subject_id的数据都连接起来
![a0290320cc902caf0d61bab037cc11d2.png](https://i-blog.csdnimg.cn/blog_migrate/d23f5e09915412af0217a0aabac4a79b.png)
#按subject_id,student_id,score来进行分组; #添加count(a.subject_id)来进行对比易于理解
分组后再进行条件查询;
接下来就是排序:
![baa64531c60c88e70a33a819ac0d8361.png](https://i-blog.csdnimg.cn/blog_migrate/e2c92694f39a83938ebb20eeb5efad6f.png)