Home > Database > Mysql Tutorial > 一个表中重复数据的处理

一个表中重复数据的处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 14:58:04
Original
1025 people have browsed it

查询、删除重复数据的SQL语句 MySQL 查询 columnName 有重复的数据,保留 id 最大的一个。SELECT MAX(id) AS id, columnName, COUNT(columnName ) AS countNumberFROM tableNameGROUP BY columnNameHAVING countNumber 1ORDER BY countNumber DESC;删除 colum

查询 、删除重复数据的SQL语句 MySQL
查询   columnName 有重复的数据,保留  id  最大的一个。

SELECT MAX(id) AS id, columnName, COUNT(columnName  ) AS countNumber
FROM tableName
GROUP BY columnName
HAVING countNumber> 1
ORDER BY countNumber DESC;


删除  columnName  有重复的数据,保留  id  最小的一个。

DELETE tableName t1
FROM tableName t1, (SELECT min(id) id , columnName
 FROM tableName
 GROUP BY columnName
 HAVING COUNT(1) > 1
 ) t2
WHERE t1.columnName = t2.columnName
AND t1.   id > t2.id;
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why does it still jump after returning false?
From 1970-01-01 08:00:00
0
0
0
Optimize table indexes in MySQL
From 1970-01-01 08:00:00
0
0
0
Why can't I install it?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template