我正在寻找有关特殊用户事件的数据库。 如果PID和UID整体相同,则必须清除表中的数据库整体。 我的服务器安装了mysql 5.x,因此CET / Over()分析功能无法正常工作。
我们非常感谢您的帮助。
Table
ID PID UID
1 1 1
2 1 1
3 1 1
4 1 1
5 3 2
6 2 5
清理后所需的表
ID PID UID
1 1 1
5 3 2
6 2 5
答案 0 :(得分:2)
您可以将delete
与join
一起使用。如果要为所有重复的对保留一行:
delete t
from t join
(select pid, uid, min(id) as min_id, count(*) as cnt
from t
group by pid, uid
) tt
on tt.pid = t.pid and t.uid = tt.uid
where t.id <> tt.min_id;
如果要删除所有 个重复项,请将where
更改为:
where cnt >= 2
答案 1 :(得分:1)
尝试如下
DELETE n1 FROM table n1 join
table n2 on n1.id > n2.id AND n1.PID = n2.PID and n1.UID=n2.UID
答案 2 :(得分:1)
使用自我加入:
delete t
from tablename t inner join tablename tt
on tt.pid = t.pid and tt.uid = t.uid
where tt.id < t.id
请参见demo。
结果:
| id | pid | uid |
| --- | --- | --- |
| 1 | 1 | 1 |
| 5 | 3 | 2 |
| 6 | 2 | 5 |