2016-03-07 108 views
0

我的记录是:如何在mysql中选择所有重复记录的

name | id | AVG(point) as point 
a  | 1 | 6 
b  | 2 | 6 
c  | 3 | 5 
d  | 4 | 5 
e  | 5 | 4 
f  | 6 | 3 
g  | 7 | 2 

如何选择以下记录:

1.I要选择前3名的记录,结果如下:

name | id | AVG(point) as point 
a  | 1 | 6 
b  | 2 | 6 
c  | 3 | 5 
d  | 4 | 5 
e  | 5 | 4 

2.我想选择记录未进前3名,结果如下:

name | id | AVG(point) as point 
f  | 6 | 3 
g  | 7 | 2 

我该怎么办?

+0

你尝试过什么?不要指望在这里发布你的作业,并有人为你回答。你应该首先谷歌解决这样的简单问题。 –

+0

不完全。它不是家庭作业。我的查询相当复杂。这个帖子很简单,从别人的建议结果。另外,在sql server中,我可以用'select top'解决问题。 –

回答

2

有几种方法可以做到这些。这里有一对夫妇使用innot in

对于前3名,你可以使用in

select * 
from yourtable 
where point in (select distinct point 
       from yourtable 
       order by 1 desc 
       limit 3) 

至于其他,使用not in代替:

select * 
from yourtable 
where point not in (select distinct point 
       from yourtable 
       order by 1 desc 
       limit 3) 

其他方法包括existsnot existsdistinctjoins

+0

我试过了,但查询返回错误:'这个版本的MySQL尚不支持'LIMIT&IN/ALL/ANY/SOME子查询' –

+0

听起来像你需要更新版本的MySQL。你有没有尝试更新? –

+0

问题是我的主机使用5.6版本,我也是。 @MatthewCliatt –

0
select * 
from yourtable as t1 
inner join (select distinct point 
       from yourtable 
       order by 1 desc 
       limit 3) as t2 
on t1.point = t2.point 

对于你的问题的第二部分,不要使用

desc