需求描述
doc表有很多重复的title,想去除掉重复的记录
表结构
CREATE TABLE doc (
id INT PRIMARY KEY,
title VARCHAR(255),
content TEXT
);
去重SQL
-- 创建临时表
CREATE TEMPORARY TABLE temp_doc AS
SELECT * FROM doc
WHERE 1=0;
-- 插入唯一的记录(每个title最小的id)
INSERT INTO temp_doc
SELECT *
FROM doc d
WHERE id IN (
SELECT MIN(id)
FROM doc
GROUP BY title
);
-- 删除原始表中的所有记录
DELETE FROM doc;
-- 将临时表中的记录插入回原始表中
INSERT INTO doc
SELECT * FROM temp_doc;
-- 删除临时表
DROP TEMPORARY TABLE temp_doc;
查看是否有重复的记录
SELECT id, title, COUNT(*)
FROM doc
GROUP BY title
ORDER BY COUNT(*) DESC;