欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

查询重复数据的几种方式

程序员文章站 2022-06-02 16:00:09
...

数据库内容如下图所示(表的名称为stu)

查询重复数据的几种方式


1.列出重复的名字

SELECT NAME FROM stu GROUP BY NAME HAVING COUNT(id)>1

2.列出名字重复的所有记录

方法一:

SELECT * FROM stu WHERE NAME IN(SELECT NAME FROM stu GROUP BY NAME HAVING COUNT(id)>1)

方法二:

SELECT s1.* FROM stu s1 WHERE (SELECT COUNT(s2.`id`) FROM stu s2 WHERE s1.`name`=s2.`name`) >1

方法三:

SELECT s1.* FROM stu s1 WHERE EXISTS (SELECT s2.* FROM stu s2 WHERE s2.`name`=s1.`name` AND s2.`id`<>s1.`id`)

方法四:

SELECT s1.* FROM stu s1 INNER JOIN stu s2 ON s1.`name`=s2.`name` AND s1.`id`<>s2.`id` GROUP BY s1.id


相关标签: 重复数据