Mysql 查询重复数据:
1.单条件查询:
SELECT id,model_id,reason FROM score WHERE type=1 GROUP BY user_id HAVING COUNT(*) > 1;
2.多条件查询:
SELECT id FROM score a WHERE
( a.user_id, a.reason, a.model_id ) IN ( SELECT user_id, reason, model_id FROM score GROUP BY user_id, reason, model_id HAVING count(*) > 1 )
3.删除重复数据,只保留最小ID的数据:(多条件)
DELETE FROM score WHERE id IN (
SELECT z.id FROM (
SELECT
id
FROM
score a
WHERE
( a.user_id, a.reason, a.model_id ) IN ( SELECT user_id, reason, model_id FROM score GROUP BY user_id, reason, model_id HAVING count(*) > 1 )
AND id NOT IN (
SELECT
min( id )
FROM
score
GROUP BY
user_id, reason, model_id
HAVING
count(*)> 1)
)z
)