如何用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问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘 ...
随机推荐
- cat命令汇总整理
Cat命令:一般用作打开文件,查看文件内容(可以一次查看多个文件),参数有如下几个: -a 或 –all,显示全部 -b 或--number-nonblank 对非空输出行编号 -n 或 --numb ...
- Shiro入门
Shiro是由Apache提供的一个强大且易用的Java安全开源框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到 ...
- Educational Codeforces Round 27 补题
题目链接:http://codeforces.com/contest/845 A. Chess Tourney 水题,排序之后判断第n个元素和n+1个元素是不是想等就可以了. #include < ...
- Nuget安装nupkg文件
问题描述: VS出现故障不能使用Manage NuGet Package自动下载安装DLL 解决方案: 直接官网下载相关需要的DLL 在VS中使用console加载本地下载的包安装 控制台code参考 ...
- 超简单使用批处理(batch)操作数据库
超简单使用批处理(batch)操作数据库 批处理(batch)是什么 批处理的执行就好比快递员的工作: 未使用批处理的时候,快递员一次从分发点将一件快递发给客户: 使用批处理,则是快递员将所有要派送的 ...
- SPOJ 104 HIGH - Highways 生成树计数
题目链接:https://vjudge.net/problem/SPOJ-HIGH 解法: 生成树计数 1.构造 基尔霍夫矩阵(又叫拉普拉斯矩阵) n阶矩阵 若u.v之间有边相连 C[u][v]=C[ ...
- spring mvc:exclude-mapping错误提示
今天搭建一个java web项目时,增加了一个登录的拦截器,主要功能就是未登录的用户无法访问系统的任何页面. 先说明下我的web项目springmvc的版本以及刚开始配置的拦截器: springmvc ...
- C语言运算符运算顺序判断实例1
程序1 #include <stdio.h> int main(void) { , j = , k = ; printf("%d\n", --j > i & ...
- java 得到以后的日期
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt222 import java.text.ParseException; im ...
- grunt之connect、watch
先说下这两个插件配合的用处,简单的说,它们可以拯救你的F5.connect用于建立一个静态服务器,watch监听文件的修改并自动实时刷新浏览器的页面. 还是options走起. connect(V0. ...