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.我想选择前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

我该怎么办?

最佳答案

有多种方法可以实现这些目的。这是使用 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 contains 以及 distinctjoins

关于mysql - 如何在mysql中选择所有重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35835145/

相关文章:

MySQL:错误 1215:无法添加外键约束

mysql - Laravel 4 远方 Eloquent 关系

mysql - CSV 文件从 Excel 到 MySQL 的字符编码

php - 如何使用php在mysql中设置变量字段名称?

mysql - phpMyadmin 备份和恢复数据库

php - 从数据库中检索数据并在得到分隔符时分隔数据?

php - 在 Moodle 3.5 中添加新用户时出错

php - php(mysql)中的FIFO计算

mysql - 非主 Grails 的重复条目问题

mysql - SQL 选择不同但详尽的列