mysql - 删除除一条记录之外的所有重复记录

时间:2014-01-18 10:08:25

标签: mysql

我想从我的表中删除除一条记录(tbl_user_points)

之外的所有重复记录

表格数据

id  userid  points  Reason
------------------------------
132 1278    50  Bonus points
153 1278    50  Bonus points
174 1278    50  Bonus points
195 1278    50  Bonus points

所以我的最终输出将是

id  userid  points  Reason
------------------------------
132 1278    50  Bonus points

请帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

试试这个

   Delete
       t1
    FROM 
       tTable t1, tTable t2 
    WHERE 
       t1.userid  = t2.userid  AND 
       t1.points  = t2.points  AND 
       t1.Reason  = t2.Reason AND 
       t1.id < t2.id

<强>(OR)

这将始终保留具有最低ID的行 -

  DELETE t2
    FROM `table` t1
    INNER JOIN `table` t2
        ON t1.userid  = t2.userid  AND 
           t1.points  = t2.points  AND 
           t1.Reason  = t2.Reason AND 
           t1.id < t2.id

答案 1 :(得分:0)

尝试:

DELETE FROM tbl_user_points
WHERE id NOT IN (
    SELECT id FROM tbl_user_points GROUP BY userid, points, Reason
)

答案 2 :(得分:0)

请试一试。

DELETE FROM tbl_user_points
    WHERE id NOT IN (
        SELECT * FROM(SELECT id FROM tbl_user_points1 GROUP BY userid, points, reason
    ) AS p)