下面以查询每门课程分数最高的学生以及成绩为例,演示如何查询 top N记录。下图是测试数据,表结构和相关 insert 脚本见《常用SQL之日期格式化和查询重复数据》。

使用自连接【推荐】

select a.name,a.course,a.score from test1 a,
(select course,max(score) score from test1 group by course) b
WHERE a.course=b.course and a.score=b.score;

执行后,结果集如下:

使用相关子查询

select name,course,score from test1 a
where a.score=(select max(score) from test1 where a.course=test1.course); 或者 select name,course,score from test1 a
where not exists(select 1 from test1 where a.course=course and a.score < score); 或者 select a.* from test1 a
where 1>(select count(*) from test1 where course=a.course and score>a.score);

结果集同上图。需要注意的是如果最高分有多条,会全部查出!

TOP N(N>1)

以N=2为例,演示如何查询TOP N(N>1)。

使用union all

如果结果集比较小,可以用程序查询单个分组结果后拼凑,也可以使用 union all。

(select name,course,score from test1 where course='语文' order by score desc limit 2)
union all
(select name,course,score from test1 where course='数学' order by score desc limit 2)
union all
(select name,course,score from test1 where course='英语' order by score desc limit 2);

自身左连接

select a.name,a.course,a.score
from test1 a left join test1 b on a.course=b.course and a.score<b.score
group by a.name,a.course,a.score
having count(b.id)<2
order by a.course,a.score desc;

两个表做连接查询,笛卡尔积,然后用having count(b.id)筛选出比当前条数大的条数。

自关联+count()

select a.* from test1 a
where 2>(select count(*) from test1 where course=a.course and score>a.score)
order by a.course,a.score desc;

思路就是判断每一条记录,比a中当前记录大的条数是否为2,如果有2条比较大,则符合。筛选出全部记录,最后按课程和学分排序。但是子查询进行了n次count(*)计算,因此性能极差。

半连接+count()+having

select * from test1 a where exists(select count(*) as sum from
test1 b where b.course=a.course and b.score>a.score having sum <2)
order by a.course,a.score desc;

MySQL查询top N记录的更多相关文章

  1. mysql 查询一条记录的下一条和上一条记录

    如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from tab ...

  2. mysql 查询随机条记录的sql语句和php计算概率

    最近在网上找了下mysql查询随机的几个sql,我把最终的记录下来. SELECT * FROM uchome_mtag AS a JOIN (SELECT MAX(tagid) AS id FROM ...

  3. mysql查询-不存在记录时赋对应的数据

    使用mysql数据库,执行查询的时候,有时候就不存在记录,但是正好在不存在记录的时候又需要给赋予相应的查询结果字段,代码实现如下: select IFNULL(('), '1970-01-01 00: ...

  4. Mysql 查询重复的记录

    我们都会使用distinct去除重复值,今天调试一个问题,业务需要查询出重复的数据,实现如下: 查询帖子的被哪些用户收藏,其中user_id,post_id可以唯一确定一条记录: 先使用post_id ...

  5. mysql 查询 TOP N 问题

    Q:有一个学生成绩表,表名 stu(学生表),字段有:id(主键),name(学生姓名),subject(学科),score(分数) 1.查询该表中,所有科目都及格的学生 ; 说明:都及格的话,就是最 ...

  6. MySQL查询重复出现次数最多的记录

    MySQL查询的方法很多,下面为您介绍的MySQL查询语句用于实现查询重复出现次数最多的记录,对于学习MySQL查询有很好的帮助作用. 在有些应用里面,我们需要查询重复次数最多的一些记录,虽然这是一个 ...

  7. mysql查询在一张表不在另外一张表的记录

    mysql查询在一张表不在另外一张表的记录   问题:    查询一个表(tb1)的字段记录不在另一个表(tb2)中      条件:tb1的字段key的值不在tbl2表中      -------- ...

  8. mysql 查询每个分组前N条记录

    mysql 查询每个分组前N条记录 假设存在表movie,  有字段 id, part(地区), mcount(观看次数) 现查询每个地区观看次数最多的3部movie, 则表 ###id虽未存在gro ...

  9. MySQL查询数据表中数据记录(包括多表查询)

    MySQL查询数据表中数据记录(包括多表查询) 在MySQL中创建数据库的目的是为了使用其中的数据. 使用select查询语句可以从数据库中把数据查询出来. select语句的语法格式如下: sele ...

随机推荐

  1. (九)Activitivi5之使用 RuntimeService 设置和获取流程变量

    一.案例 /** * 设置流程变量数据 */ @Test public void setVariableValues(){ RuntimeService runtimeService=processE ...

  2. 注解@Slf4j使用

    我们在写代码的时候需要加入日志打印,如果不想每次都写private  final Logger logger = LoggerFactory.getLogger(XXX.class); 那么可以用注解 ...

  3. elementui 树控件只隐藏第三集菜单

    <!-- 必须属性:default-expanded-keys node-key --> <el-tree :default-expanded-keys='idArr' node-k ...

  4. PHP常见函数

    有时候,运行nginx和PHP CGI(PHP FPM)web服务的Linux服务器,突然系统负载上升,用top命令查看,很多phpcgi进程的CPU利用率接近100%后来通过跟踪发现,这种情况与PH ...

  5. 数据库之sqlite

    数据创建数据 CREATE TABLE IF NOT EXISTS ArpAudit (ID INTEGER PRIMARY KEY autoincrement NOT NULL, UserName ...

  6. Linux上使用trash回收机制来替换rm命令

    因为我们日常使用的rm 命令没有恢复机制,删除了文件就找不到了,往往重要的文件,我们要特别小心才对,但是有时还是避免不了我们的误操作.可能会造成很大的影响. 本博文简单介绍一下,用trash命令仿照W ...

  7. TSN(Temporal Segment Networks)

    一.算法详解 二.代码解析(pytorch版) 训练代码:https://blog.csdn.net/u014380165/article/details/79058147 测试代码:https:// ...

  8. PAT Advanced 1073 Scientific Notation (20 分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  9. 若干简单的进程和作业调度的C++模拟程序

    进程调度(时间片轮转,动态优先级,链表形式): #include<cstdio> #include<cstdlib> struct PCB { ]; char state; / ...

  10. Til the Cows Come Home(Dijkstra)

    Dijkstra (迪杰斯特拉)最短路算法,算是模板 POJ - 2387 #include<iostream> #include<algorithm> #include< ...