当前位置: 首页 > news >正文

mysql 取top 10_mysql分组取topn

本文来自  http://www.jb51.net/article/31590.htm

有些语句sql top n 是sqlserver语法

--按某一字段分组取最大(小)值所在行的数据

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*

数据如下:

name val memo

a 2 a2(a的第二个值)

a 1 a1--a的第一个值

a 3 a3:a的第三个值

b 1 b1--b的第一个值

b 3 b3:b的第三个值

b 2 b2b2b2b2

b 4 b4b4

b 5 b5b5b5b5b5

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

--创建表并插入数据:

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

create table tb(name varchar(10),val int,memo varchar(20))

insert into tb values('a', 2, 'a2(a的第二个值)')

insert into tb values('a', 1, 'a1--a的第一个值')

insert into tb values('a', 3, 'a3:a的第三个值')

insert into tb values('b', 1, 'b1--b的第一个值')

insert into tb values('b', 3, 'b3:b的第三个值')

insert into tb values('b', 2, 'b2b2b2b2')

insert into tb values('b', 4, 'b4b4')

insert into tb values('b', 5, 'b5b5b5b5b5')

go

48304ba5e6f9fe08f3fa1abda7d326ab.png

--一、按name分组取val最大的值所在行的数据。

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

--方法1:select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name

--方法2:

select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)

--方法3:

select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name

--方法4:

select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name

--方法5

select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name

/*

name val memo

---------- ----------- --------------------

a 3 a3:a的第三个值

b 5 b5b5b5b5b5

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

本人推荐使用1,3,4,结果显示1,3,4效率相同,2,5效率差些,不过我3,4效率相同毫无疑问,1就不一样了,想不搞了。

--二、按name分组取val最小的值所在行的数据。

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

--方法1:select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name

--方法2:

select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)

--方法3:

select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name

--方法4:

select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name

--方法5

select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name

/*

name val memo

---------- ----------- --------------------

a 1 a1--a的第一个值

b 1 b1--b的第一个值

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

--三、按name分组取第一次出现的行所在的数据。

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name

/*

name val memo

---------- ----------- --------------------

a 2 a2(a的第二个值)

b 1 b1--b的第一个值

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

--四、按name分组随机取一条数据。

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name/*

name val memo

---------- ----------- --------------------

a 1 a1--a的第一个值

b 5 b5b5b5b5b5

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

--五、按name分组取最小的两个(N个)val

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.valselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val

select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name

/*

name val memo

---------- ----------- --------------------

a 1 a1--a的第一个值

a 2 a2(a的第二个值)

b 1 b1--b的第一个值

b 2 b2b2b2b2

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

--六、按name分组取最大的两个(N个)val

代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val

select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val

select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name

/*

name val memo

---------- ----------- --------------------

a 2 a2(a的第二个值)

a 3 a3:a的第三个值

b 4 b4b4

b 5 b5b5b5b5b5

*/

48304ba5e6f9fe08f3fa1abda7d326ab.png

相关文章:

  • kvo实现原理_详细了解Cocoa中KVO机制是什么
  • gitee如何搭建mysql_gitee中项目到运行操作,包括:打包、热部署、数据库操作
  • php mysql 并发写入_php+mysql高并发插入数据重复问题!
  • python pickle模块_python pickle 模块的使用
  • python 曲线拟合参数能否为数组_Python曲线将多个参数拟合到多个数据集
  • plsql trim去不掉空格_Excel中使用TRIM与CALEN都无法清除空格与不可见字符时怎么办...
  • java日历算法分析_Java基础算法分析之一
  • ssh连接docker vscode_VScode远程连接Docker容器实现X11转发
  • linux mysql 最全安装_最全的mysql 5.7.13 安装配置方法图文教程(linux) 强烈推荐!
  • centos7镜像带mysql吗_【系列7】使用Dockerfile创建带mysql的Centos Docker镜像
  • spark mysql 环境搭建_Hive搭建
  • mysql vsftp_vsftp配置验证方式mysql和文件虑拟用户
  • php mysql 连接运算符_php – MINUS运算符在MySQL?
  • centos7.3中安装mysql_CentOS7.3中安装MySQL5.6
  • qstring取前几个_QT中QString 类的使用--获取指定字符位置、截取子字符串等
  • 【刷算法】从上往下打印二叉树
  • 0x05 Python数据分析,Anaconda八斩刀
  • create-react-app项目添加less配置
  • MD5加密原理解析及OC版原理实现
  • October CMS - 快速入门 9 Images And Galleries
  • Promise面试题2实现异步串行执行
  • SegmentFault 2015 Top Rank
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • vue中实现单选
  • Web设计流程优化:网页效果图设计新思路
  • Xmanager 远程桌面 CentOS 7
  • 关于List、List?、ListObject的区别
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 那些被忽略的 JavaScript 数组方法细节
  • 爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
  • 浅谈web中前端模板引擎的使用
  • 设计模式走一遍---观察者模式
  • 项目管理碎碎念系列之一:干系人管理
  • 小李飞刀:SQL题目刷起来!
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 《码出高效》学习笔记与书中错误记录
  • gunicorn工作原理
  • Spring第一个helloWorld
  • ​2021半年盘点,不想你错过的重磅新书
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (arch)linux 转换文件编码格式
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (Forward) Music Player: From UI Proposal to Code
  • (多级缓存)缓存同步
  • (二)springcloud实战之config配置中心
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (蓝桥杯每日一题)love
  • (数据结构)顺序表的定义
  • (顺序)容器的好伴侣 --- 容器适配器
  • .net core控制台应用程序初识