MYSQL 查询重复数据
Posted 知其黑、受其白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL 查询重复数据相关的知识,希望对你有一定的参考价值。
阅读目录
mysql 查询重复数据
1 查询 people_no 重复的记录
SELECT
*
FROM
people
WHERE
people_no IN (
SELECT
people_no
FROM
people
GROUP BY
people_no
HAVING
count(people_no) > 1
);
2 查询 people_no 重复的记录 ,排除最小 id,如果删除改为delete from
SELECT
*
FROM
people
WHERE
people_no IN (
SELECT
people_no
FROM
people
GROUP BY
people_no
HAVING
count(people_no) > 1
)
AND id NOT IN (
SELECT
min(id)
FROM
people
GROUP BY
people_no
HAVING
count(people_no) > 1
);
3 查询 people_no、people_name 重复的记录
SELECT
*
FROM
people a
WHERE
(a.people_no, a.people_name) IN (
SELECT
people_no,
people_name
FROM
people
GROUP BY
people_no,
people_name
HAVING
count(*) > 1
);
4 查询 people_no、people_name 重复的记录,排除最小 id
SELECT
*
FROM
people a
WHERE
(a.people_no, a.people_name) IN (
SELECT
people_no,
people_name
FROM
people
GROUP BY
people_no,
people_name
HAVING
count(*) > 1
)
AND a.id NOT IN (
SELECT
min(id)
FROM
people
GROUP BY
people_no,
people_name
HAVING
count(*) > 1
);
以上是关于MYSQL 查询重复数据的主要内容,如果未能解决你的问题,请参考以下文章