1

我想删除最后一行,因为 userid 和friendwith 列是重复的。

friendshipid    userid  friendwith  friendshipstatus
183             24      102          4
151             24      52           2
155             24      66           2
179             24      66           2

谢谢。

4

3 回答 3

3

如果您想保留最新的友谊ID,请执行以下操作

CREATE TABLE temp_table AS (SELECT * FROM table);
DELETE FROM table WHERE friendshipid NOT IN (SELECT friendshipid FROM (SELECT * FROM temp_table ORDER BY friendshipid DESC) as temp_table GROUP BY userid, friendwith);
DROP TABLE temp_table ;

或者,如果您想保留最古老的友谊 ID,请执行以下操作

CREATE TABLE temp_table AS (SELECT * FROM table);
DELETE FROM table WHERE friendshipid NOT IN (SELECT friendshipid FROM (SELECT * FROM temp_table ORDER BY friendshipid ASC) as temp_table GROUP BY userid, friendwith);
DROP TABLE temp_table ;
于 2012-04-15T09:11:30.670 回答
1

userid您可以使用相同的and删除所有存在另一行的行friendswith,但使用较低的friendshipid. 例如:

delete  dup
from    YourTable as dup
join    YourTable orig
on      orig.userid = dup.userid
        and orig.friendwith = dup.friendwith
        and orig.friendshipid < dup.friendshipid

SQL Fiddle 的示例。

于 2012-04-15T09:10:47.573 回答
0
select friendshipid  ,  userid  , friendwith  , friendshipstatus from table 
group by userid , friendwith  
于 2012-04-15T09:05:48.967 回答