一、需求
MYSQL group by后删除每个分组中的重复数据,只保留最新一条
二、实现
#获取 group by后每个分组中除去最新一条记录的其他重复数据,
SELECT * FROM test WHERE test_user_id IN(
#按照test_user_id分组,获取每个分组中记录大于一条记录的test_user_id;
#外层查询test_user_id IN(),即可查询到分组大于一条记录的test_user_id对应的所有数据
SELECT test_user_id FROM test GROUP BY test_user_id HAVING COUNT(*)>1
)
AND test_id NOT IN(
#按照test_user_id分组,获取每个分组中最新的一条记录;
#外层查询test_id NOT IN()即会过滤每个分组中最新的一条记录
SELECT MAX(test_id) FROM test GROUP BY test_user_id HAVING COUNT(*)>1
);
DELETE FROM test WHERE test_user_id IN(
SELECT test_user_id FROM test GROUP BY test_user_id HAVING COUNT(*)>1
)
AND test_id NOT IN(
SELECT MIN(test_id) FROM test GROUP BY test_user_id HAVING COUNT(*)>1
);
#上面的sql会报错“You can't specify target table 'candidate_information' for update in FROM clause
”,优化为如下sql:
DELETE FROM test WHERE test_user_id IN(
SELECT test_user_id FROM (
SELECT test_user_id FROM test GROUP BY test_user_id HAVING COUNT(*)>1
) AS a
)
AND test_id NOT IN(
SELECT id FROM (
SELECT MIN(test_id) AS id FROM test GROUP BY test_user_id HAVING COUNT(*)>1
) AS b
);