mysql 数据库去重

DELETE consum_record
FROM
consum_record,
(
SELECT
min(id) id,
user_id,
monetary,
consume_time
FROM
consum_record
GROUP BY
user_id,
monetary,
consume_time
HAVING
count(*) > 1
) t2
WHERE
consum_record.user_id = t2.user_id
AND consum_record.monetary = t2.monetary
AND consum_record.consume_time = t2.consume_time
AND consum_record.id > t2.id;
首先是将重复的记录查出来另存到一个集合(临时表t2), 集合里是每种重复记录的最小ID

consum_record.user_id = t2.user_id
AND consum_record.monetary = t2.monetary
AND consum_record.consume_time = t2.consume_time
AND consum_record.id > t2.id

你可能感兴趣的:(mysql 数据库去重)