chatgpt: 问题:
mysql 想在表中只保留一份,现在使用sql:select cyber_star_name,count(0) c from cyber_star_info GROUP BY cyber_star_name HAVING c > 1 查询出来的这些,如何删除重复的,只保留一份呢
-- 创建临时表用于存储需要删除的重复记录的主键
CREATE TEMPORARY TABLE temp_table
SELECT MIN(id) AS min_id
FROM cyber_star_info
GROUP BY cyber_star_name
HAVING COUNT(*) > 1;
-- 删除重复记录
DELETE FROM cyber_star_info
WHERE id NOT IN (SELECT min_id FROM temp_table);
-- 删除临时表
DROP TEMPORARY TABLE IF EXISTS temp_table;