如何用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问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘 ...
随机推荐
- Promise与异步
不知道promise,大家现在用了吗?如果还不了解的话,今天就来对了-基础的了解起来- 正文从这开始- 接触过promise的的都知道它的应用场景和用途,Promise可以用来避免异步操作函数里的嵌套 ...
- 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(四)example代码解析
一.example逻辑伪码 myRTSPClient附带3个example程序:simple_example.complete_example.common_example.后两个example都是从 ...
- Centos7.2 编译安装方式搭建 phpMyAdmin
1. 下载 编译 安装 pcre tar zxvf pcre-8.41.tar.gz cd pcre-8.41 ./configure --prefix=/opt/local/pcre-8.41 ma ...
- c语言 创建链表
#include "malloc.h" #include "stdio.h" #define LEN sizeof(struct student) typede ...
- spring框架整合hibernate框架简单操作数据库
1.配置文件: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http:/ ...
- LaTeX的表格插入与排版
关于LaTex中的图表问题的几篇有用的文章 Graphics and Colour with LaTeX 有关在LaTex中插图和使用颜色的在线指导. Figure'ing and Picture' ...
- STL中map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- 打印1到最大的n位数-Java
在练习剑指offer的时候,第12题打印1到最大的n位数的时候,想找个java版的,但大家要么用BigInteger做,要么给出其他的方法.我觉得要给就给最好的方法,下面是我自己参考C++代码写的ja ...
- spring MVC处理请求过程及配置详解
本文主要梳理下Spring MVC处理http请求的过程,以及配置servlet及业务application需要的常用标签,及其包含的意义. spring MVC处理请求过程 首先看一个整体图 简单说 ...
- LNMP1.4环境中安装fileinfo插件
安装前: 安装前建议先执行 /usr/local/php/bin/php -m (此命令显示目前已经安装好的PHP模块)看一下,要安装的模块是否已安装. 然后下载当前PHP版本的源码并解压. 安装: ...