1、查找重复数据:
SELECT * FROM `table_a` as a WHERE NOT EXISTS (SELECT * FROM table_b as b WHERE b.a_id=a.id)) as c GROUP BY c.title HAVING count(1) > 1;
2、查找重复数据中需要保留的数据:
SELECT MIN(c.id) as id FROM (SELECT * FROM `table_a` as a WHERE NOT EXISTS (SELECT * FROM table_b as b WHERE b.a_id=a.id)) as c GROUP BY c.title HAVING count(1) > 1;
3、删除重复数据保留一条数据:
DELETE FROM table_a WHERE title in ( SELECT title FROM (SELECT * FROM `table_a` as a WHERE NOT EXISTS (SELECT * FROM table_b as b WHERE b.a_id=a.id)) as c GROUP BY c.title HAVING count(1) > 1 ) AND id NOT in ( SELECT MIN(c.id) as id FROM (SELECT * FROM `table_a` as a WHERE NOT EXISTS (SELECT * FROM table_b as b WHERE b.a_id=a.id)) as c GROUP BY c.title HAVING count(1) > 1);
解决mysql中重复数据又想保留一条数据的思路。