如何在mysql中选择所有重复记录
我的记录是:如何在mysql中选择所有重复记录,mysql,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.我想选择前三名记录,结果如下: name | id |
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.我想选择前三名记录,结果如下:
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
我该怎么做?有几种方法可以做到这一点。这里有一对夫妇在中使用了
,而在
中没有使用
对于前三名,您可以使用中的:
select *
from yourtable
where point in (select distinct point
from yourtable
order by 1 desc
limit 3)
对于其余部分,请改用不在中:
select *
from yourtable
where point not in (select distinct point
from yourtable
order by 1 desc
limit 3)
其他方法包括存在
与不存在
和不同
与连接
有几种方法可以做到这一点。这里有一对夫妇在
中使用了,而在
中没有使用
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
对于前三名,您可以使用中的:
select *
from yourtable
where point in (select distinct point
from yourtable
order by 1 desc
limit 3)
对于其余部分,请改用不在中:
select *
from yourtable
where point not in (select distinct point
from yourtable
order by 1 desc
limit 3)
其他方法包括存在
与不存在
和不同
与连接
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
对于问题的第二部分,不要使用
desc
你试过什么?不要期望把你的作业贴在这里,让别人帮你回答。你应该先用谷歌来解决像这样的简单问题。不完全是。这不是家庭作业。我的查询相当复杂。这篇文章是某人建议的简单结果。此外,在sql server中,我可以用选择top
来解决问题。你尝试过什么?不要期望把你的作业贴在这里,让别人帮你回答。你应该先用谷歌来解决像这样的简单问题。不完全是。这不是家庭作业。我的查询相当复杂。这篇文章是某人建议的简单结果。此外,在sql server中,我可以用选择top
来解决问题。我已经尝试过,但是查询返回错误:这个版本的MySQL还不支持“LIMIT&IN/ALL/ANY/SOME子查询”
听起来你需要一个更新版本的MySQL。你试过更新吗?问题是我的主机使用的是5.6版,我也是@MatthewCliatt你必须用一句话吗?或者您可以使用存储过程?我认为在这种情况下使用存储过程不是最好的方法。否则,我将在下次尝试使用循环和测试@PeterI已经尝试过了,但是查询返回错误:这个版本的MySQL还不支持“LIMIT&IN/ALL/ANY/SOME子查询”
听起来你需要一个更新版本的MySQL。你试过更新吗?问题是我的主机使用的是5.6版,我也是@MatthewCliatt你必须用一句话吗?或者您可以使用存储过程?我认为在这种情况下使用存储过程不是最好的方法。否则,我将在下次尝试使用循环和测试@彼得