Home > Database > Mysql Tutorial > body text

查询分组后每个分组的前几条记录

WBOY
Release: 2016-06-07 16:24:16
Original
1504 people have browsed it

在MySQL使用中,经常需要查询每个分组的前几条记录(查询分组后每一个组的前几项),下面写了个简单的例子说明下SQL的写法。简单的表设计如下,要求每个班总分排名最前的前两条数据。 测试表语句如下: create table test(id int unsigned not null auto_inc

在MySQL使用中,经常需要查询每个分组的前几条记录(查询分组后每一个组的前几项),下面写了个简单的例子说明下SQL的写法。简单的表设计如下,要求每个班总分排名最前的前两条数据。

测试表语句如下:

create table test(id int unsigned not null auto_increment primary key,name varchar(10),class varchar(20),score varchar(20));
insert into test(name, class, score) values ('gonn', '6(1)', '299');
insert into test(name, class, score) values ('yyun', '6(1)', '259');
insert into test(name, class, score) values ('lin', '6(1)', '289');
insert into test(name, class, score) values ('mei', '6(1)', '277');
insert into test(name, class, score) values ('xj', '6(2)', '287');
insert into test(name, class, score) values ('zhl', '6(2)', '277');
insert into test(name, class, score) values ('lwjs', '6(2)', '257');
insert into test(name, class, score) values ('lulu', '6(2)', '265');
Copy after login

运行以上SQL,得到的表结构如下:

mysql> SELECT * FROM test;
+----+------+-------+-------+
| id | name | class | score |
+----+------+-------+-------+
|  1 | gonn | 6(1)  | 299   |
|  2 | yyun | 6(1)  | 259   |
|  3 | lin  | 6(1)  | 289   |
|  4 | mei  | 6(1)  | 277   |
|  5 | xj   | 6(2)  | 287   |
|  6 | zhl  | 6(2)  | 277   |
|  7 | lwjs | 6(2)  | 257   |
|  8 | lulu | 6(2)  | 265   |
+----+------+-------+-------+
8 rows in set
Copy after login

方法一

mysql> SELECT a.id,a.name,a.class,a.score
FROM test a LEFT JOIN test b on a.class = b.class and a.score < b.score
GROUP BY a.id,a.name,a.class,a.score
HAVING count(b.id) < 2
ORDER BY a.class,a.score DESC;
+----+------+-------+-------+
| id | name | class | score |
+----+------+-------+-------+
|  1 | gonn | 6(1)  | 299   |
|  3 | lin  | 6(1)  | 289   |
|  5 | xj   | 6(2)  | 287   |
|  6 | zhl  | 6(2)  | 277   |
+----+------+-------+-------+
4 rows in set
Copy after login

方法二

mysql> SELECT * FROM test a
WHERE 2 >(SELECT count(*) FROM test WHERE class = a.class and score>a.score)
ORDER BY a.class,a.score DESC;
+----+------+-------+-------+
| id | name | class | score |
+----+------+-------+-------+
|  1 | gonn | 6(1)  | 299   |
|  3 | lin  | 6(1)  | 289   |
|  5 | xj   | 6(2)  | 287   |
|  6 | zhl  | 6(2)  | 277   |
+----+------+-------+-------+
4 rows in set
Copy after login

这里列出了多种SQL语句的实现方法,有些是MySQL特有的(Limit, 其它数据库可根据实际更改,比如oracle的rownum,MS SQL SERVER 的 top,..),有时是SQL标准支持的。但效率上和应用的场合或许不同。具体应用时可根据实际表中的记录情况,索引情况进行选择。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!