(转)mysql分组取最大(最小、最新、前N条)条记录

2019-09-20 06:10 来源:未知

6、依据Name分组取最小的两个(N个)Val

图片.png

彩民之家高手论坛 1

按name分组取val最小的值所在行的数码
方法一:

实践结果如下图:

方法四:

sql数据库达成分组并取每组的前1(几)条数据

结果如下:

运行几遍施行结果如下图:

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

上边的两种办法实行的结果一律如下图:

方法二:

彩民之家高手论坛 2

方法三:

上边包车型地铁三种方法实践结果是一致的如下图:

按name分组取最小的多个(N个)val
方法一:

测验数据希图职业:

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

3、依据Name分组取第二次面世的行所在的数据。

方法二:

select a.* from Test_orderByOrGroupBy_tb a where a.Val = (select top 1 Val from Test_orderByOrGroupBy_tb where Name = a.Name order by newid()) order by a.Name
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name

5、依照Name分组取最大的七个(N个)Val

按name分组取第一回面世的行所在的多寡
sql如下:

CREATE table Test_orderByOrGroupBy_tb(Name nvarchar(50),Val int,Describe nvarchar(50)) 
insert into Test_orderByOrGroupBy_tb values('a', 1, 'a1--a的第一个值') 
insert into Test_orderByOrGroupBy_tb values('b', 2, 'b2b2b2b2b2b2b2b2b值') 
insert into Test_orderByOrGroupBy_tb values('a', 2, 'a2(a的第二个值)') 
insert into Test_orderByOrGroupBy_tb values('b', 1, 'b1--b的第一个值') 
insert into Test_orderByOrGroupBy_tb values('a', 3, 'a3:a的第三个值') 
insert into Test_orderByOrGroupBy_tb values('b', 3, 'b3:b的第三个值') 
insert into Test_orderByOrGroupBy_tb values('c', 1, 'c1c1c1c1c1c1c1c1c1c1c值')
insert into Test_orderByOrGroupBy_tb values('b', 5, 'b5b5b5b5b5b5b5b5b5b5值') 
insert into Test_orderByOrGroupBy_tb values('c', 2, 'c2c2c2c2c2c2c2c2c2c2值') 
insert into Test_orderByOrGroupBy_tb values('b', 4, 'b4b4b4b4b4b4b4b4b值') 
GO

按name分组取最大的多个(N个)val
方法一:

上面5种办法实践结果是一样的,如下图:

方法四:

上边的5种方法的sql执行施行结果一致,结果如下图:

方法三:

--方法一:
select a.* from Test_orderByOrGroupBy_tb a where 2 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and val < a.val ) order by a.name,a.val 
--方法二:
select a.* from Test_orderByOrGroupBy_tb a where val in (select top 2 val from Test_orderByOrGroupBy_tb where name=a.name order by val) order by a.name,a.val 
--方法三
SELECT a.* from Test_orderByOrGroupBy_tb a where exists (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and val < a.val having Count(*) < 2) order by a.name 

推介使用第一、第三、第四钟方法,结果展现第1,3,4种艺术成效同样,第2,5种艺术效能差些。

Sql语句代码如下:

结果如下:

依据某一个字段分组取最大(小)值所在行的数量:

方法二:

--方法一:
select a.* from Test_orderByOrGroupBy_tb a where 2 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val ) order by a.Name,a.Val 
--方法二:
select a.* from Test_orderByOrGroupBy_tb a where val in (select top 2 val from Test_orderByOrGroupBy_tb where Name=a.Name order by Val desc) order by a.Name,a.Val 
--方法三:
SELECT a.* from Test_orderByOrGroupBy_tb a where exists (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val having Count(*) < 2) order by a.Name 
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
select a.* from Test_orderByOrGroupBy_tb a where a.Val = (select top 1 val from Test_orderByOrGroupBy_tb where a.Name = a.Name) order by a.Name 

方法三:

2、遵照Name分组取Val最小的值所在行的数额。

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

创立表并且插入数据

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彩民之家高手论坛 5彩民之家高手论坛 6

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

彩民之家高手论坛 7

select 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

1、依据Name分组取Val最大的值所在行的数量。

图片.png

4、依据Name分组随机取一条数据

彩民之家高手论坛 8

彩民之家高手论坛 9

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
--方法1:
select a.* from Test_orderByOrGroupBy_tb a where Val = (select min(Val) from Test_orderByOrGroupBy_tb where Name = a.Name) order by a.Name
--方法2: 
select a.* from Test_orderByOrGroupBy_tb a,(select Name,min(Val) Val from Test_orderByOrGroupBy_tb group by Name) b where a.Name = b.Name and a.Val = b.Val order by a.Name 
--方法3: 
select a.* from Test_orderByOrGroupBy_tb a inner join (select Name,min(Val) Val from Test_orderByOrGroupBy_tb group by Name) b on a.Name = b.Name and a.Val = b.Val order by a.Name 
--方法4: 
select a.* from Test_orderByOrGroupBy_tb a where 1 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val < a.Val ) order by a.Name --其中1表示获取分组中前一条数据
--方法5: 
select a.* from Test_orderByOrGroupBy_tb a where not exists(select 1 from Test_orderByOrGroupBy_tb where Name = a.Name and Val < a.Val)
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法1:
select a.* from Test_orderByOrGroupBy_tb a where Val = (select max(Val) from Test_orderByOrGroupBy_tb where Name = a.Name) order by a.Name
--方法2: 
select a.* from Test_orderByOrGroupBy_tb a,(select Name,max(Val) Val from Test_orderByOrGroupBy_tb group by Name) b where a.Name = b.Name and a.Val = b.Val order by a.Name 
--方法3: 
select a.* from Test_orderByOrGroupBy_tb a inner join (select Name,max(Val) Val from Test_orderByOrGroupBy_tb group by Name) b on a.Name = b.Name and a.Val = b.Val order by a.Name 
--方法4: 
select a.* from Test_orderByOrGroupBy_tb a where 1 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val ) order by a.Name --其中1表示获取分组中前一条数据
--方法5: 
select a.* from Test_orderByOrGroupBy_tb a where not exists(select 1 from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val) 
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

彩民之家高手论坛 10

图片.png

图片.png

图片.png

按name分组随机取一条数据
sql如下:

结果如下:

:http://www.manongjc.com/article/1082.html(以转的链接为主)

方法五:

在数据库开辟进度中,我们要为各类档次的数量收取前几条记下,或许是取最新、最小、最大等等,那么些该怎么促成啊,本文章向大家介绍怎么样完毕mysql分组取最大(最小、最新、前N条)条记下。必要的能够参照一下。

上述四种形式运营的结果均为如下所示:

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

彩民之家高手论坛 11

先看一下本示例中必要采纳到的多寡
始建表并插入数据:

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

图片.png

彩民之家高手论坛 12

如上多样格局运营的结果均为如下所示:

数据表如下:

彩民之家高手论坛 13

方法二:

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

结果如下:

方法五:

方法三:

彩民之家高手论坛 14

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

图片.png

create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2')
insert into tb values('a',    1,   'a1')
insert into tb values('a',    3,   'a3')
insert into tb values('b',    1,   'b1')
insert into tb values('b',    3,   'b3')
insert into tb values('b',    2,   'b2')
insert into tb values('b',    4,   'b4')
insert into tb values('b',    5,   'b5')

彩民之家高手论坛 15

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

按name分组取val最大的值所在行的数目

方法一:

彩民之家高手论坛 16

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
TAG标签: MySQL
版权声明:本文由彩民之家高手论坛发布于彩民之家高手论坛,转载请注明出处:(转)mysql分组取最大(最小、最新、前N条)条记录