如何用SQL实现组内前几名的输出
关于问题
什么是相关子查询
- 从外层查询中取出一个元组,将元组相关列的值传给内层查询
- 执行内层查询,得到子查询操作的值
- 外查询根据子查询返回的结果或结果集得到满足条件的行
- 然后外层查询取出下一个元组重复做步骤1-3,直到外层的元组全部处理完毕。
在MySQL中查询每组中的前N个值

select type, variety, price
from fruits
where (
select count(*) from fruits as f
where f.type = fruits.type and f.price < fruits.price
) <= 1;
select type, variety, price
from fruits
where (
select count(*) from fruits as f
where f.type = fruits.type and f.price < fruits.price
) <= 1;
select count(*) from fruits as f
where f.type = fruits.type and f.price < fruits.price
select count(*) from fruits as f
where f.type = fruits.type and f.price < fruits.price
select count(*) from fruits as f
where f.type = apple and f.price < 2.79
select count(*) from fruits as f
where f.type = apple and f.price < 2.79
select type, variety, price
from fruits
where (
select count(*) from fruits as f
where f.type = apple and f.price < 2.79
) <= 1;
select type, variety, price
from fruits
where (
select count(*) from fruits as f
where f.type = apple and f.price < 2.79
) <= 1;
select type, variety, price
from fruits
where (
select count(*) from fruits as f
where f.type = fruits.type and f.price < fruits.price
) <= 1
order by fruits.type, fruits.price
select type, variety, price
from fruits
where (
select count(*) from fruits as f
where f.type = fruits.type and f.price < fruits.price
) <= 1
order by fruits.type, fruits.price
参考链接
如何用SQL实现组内前几名的输出的更多相关文章
- postgres select TOP X in group 查询每个组的前几名
参考: https://stackoverflow.com/questions/27415706/postgresql-select-top-three-in-each-group http://ch ...
- mysql 分组取每个组的前几名的问题
select *from hotel_addition_orders awhere (select count(*) from hotel_addition_orders where hotel_or ...
- 如何用sql批量删除一个id段内的dedecms文章?
之前因为ytkah批量添加了dedecms文章,数量有些多,后面出现问题了,想要删除一部分织梦文章,后台一篇篇删,删到手软(相关内容:修改dedecms关键词到手软),于是就想到了sql数据库操作!那 ...
- [SQL]用于提取组内最新数据,左连接,内连接,not exist三种方案中,到底谁最快?
本作代码下载:https://files.cnblogs.com/files/xiandedanteng/LeftInnerNotExist20191222.rar 人们总是喜欢给出或是得到一个简单明 ...
- sql查询技巧,按时间分段进行分组,每半小时一组统计组内记录数量
今天拿到一个查询需求,需要统计某一天各个时间段内的记录数量. 具体是统计某天9:00至22:00时间段,每半小时内订单的数量,最后形成的数据形式如下: 时间段 订单数 9:00~9: ...
- sql 分组后显示每组的前几条记录
sql 分组后显示每组的前几条记录 如 表中记录是 code serialno A1 1 ...
- sql server迁移数据(文件组之间的互相迁移与 文件组内文件的互相迁移)
转自:https://www.cnblogs.com/lyhabc/p/3504380.html?utm_source=tuicool SQLSERVER将数据移到另一个文件组之后清空文件组并删除文件 ...
- sql 分组取每组的前n条或每组的n%(百分之n)的数据
sql 分组取每组的前n条或每组的n%(百分之n)的数据 sql keyword: SELECT * ,ROW_NUMBER() OVER(partition by b.UserID order by ...
- 在论坛中出现的比较难的sql问题:29(row_number函数 组内某列的值连续出现3次标记出来)
原文:在论坛中出现的比较难的sql问题:29(row_number函数 组内某列的值连续出现3次标记出来) 在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘 ...
随机推荐
- Spring Boot 出现 in a frame because it set 'X-Frame-Options' to 'DENY'
在spring boot项目中出现不能加载iframe 页面报一个"Refused to display 'http://......' in a frame because it set ...
- jQuery选择器使用习惯
http://www.cnblogs.com/fredlau/archive/2009/03/19/1416327.html
- spring boot 登录注册 demo (一)
Welcome to Spring Boot 代码结构 src/main/java 下 controller层,路由功能dao层,数据库的访问domain,bean的存放service,业务层appl ...
- 零碎的JS基础
一.js的三种弹窗: 警告框 弹出警告alert() 确认框 有确认内容的框confirm()有两个值,true和false 当用户按下确认键后,打印tru ...
- TortoiseSVN使用经验
TortoiseSVN使用经验.. SVN新下载的项目(有感叹号和对勾)正常的-- 没有标识--重启一下电脑, ---------------ipConfig----- SVN是用哪个IP地址chec ...
- jquery layer弹窗弹层插件 (转)
/* 先去官网下载最新的js http://sentsin.com/jquery/layer/ ①引用jquery ②引用layer.min.js */ 触发弹层的事件可自由绑定,如: $('#id ...
- 部署和使用kibana
背景 上一篇介绍了在阿里云上部署ES,本文将主要介绍ELK的可视化工具Kibana的部署和使用.主要分为三个步骤来实现最终呈现: 1.导入数据到ES: 2.部署kibana并完成配置: 3.使用kib ...
- Linux巩固记录(1) J2EE开发环境搭建及网络配置
由于要近期使用hadoop等进行相关任务执行,操作linux时候就多了 以前只在linux上配置J2EE项目执行环境,无非配置下jdk,部署tomcat,再通过docker或者jenkins自动部署上 ...
- jquery的2.0.3版本源码系列(1)总体结构
为什么选择2.X版本,而不是1.X版本,因为2.X不兼容IE6/7/8,所以少了兼容代码,让我们更专注于jquery原理的代码. 一共有8830行. 1.1 匿名函数自执行 首先,匿名函数的作用是,把 ...
- 使用Groovy处理SoapUI中Json response
最近工作中,处理最多的就是xml和json类型response,在SoapUI中request里面直接添加assertion处理json response的话,可以采用以下方式: import gro ...