我的记录是:
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.我想选择前3名记录,结果如下:
name | id | AVG(point) as point
a | 1 | 6
b | 2 | 6
c | 3 | 5
d | 4 | 5
e | 5 | 4
2.我想选择不进入前三名的记录,结果如下:
name | id | AVG(point) as point
f | 6 | 3
g | 7 | 2
我该怎么办?
答案 0 :(得分:2)
有几种方法可以做到这些。以下是使用in
和not 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)
其他方法包括exists
not exists
和distinct
joins
。
答案 1 :(得分: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