删除 MySQL 中的双向重复项
我正在修改 phpBB 的表以建立朋友的双向关系。不幸的是,已经添加朋友的人创建了重复的行:
user1 user2 friend
2 3 true
3 2 true
2 4 true
所以我想从上面的示例中删除第 1 行和第 2 行。目前,这是我构建的查询(不适用于atm):
DELETE FROM friends WHERE user1 IN (SELECT user1 FROM (SELECT f1.user1 FROM friends f1, friends f2 WHERE f1.user1=f2.user2 AND f1.user2=f2.user1 GROUP BY f1.user1) AS vtable);
受到 Mysql 重复行(使用 2 列检测到重复),但不同之处在于我没有唯一的 ID 列,并且我希望避免使用额外的列。
I'm modifying phpBB's table to have bidirectional relationships for friends. Unfortuntately, people that have already added friends have created duplicate rows:
user1 user2 friend
2 3 true
3 2 true
2 4 true
So I'd like to remove rows 1 and 2 from the example above. Currently, this is my query built (doesn't work atm):
DELETE FROM friends WHERE user1 IN (SELECT user1 FROM (SELECT f1.user1 FROM friends f1, friends f2 WHERE f1.user1=f2.user2 AND f1.user2=f2.user1 GROUP BY f1.user1) AS vtable);
inspired by Mysql Duplicate Rows ( Duplicate detected using 2 columns ), but the difference is that I don't have the unique ID column and I'd like stay away from having an extra column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,如果这不是 100% 合法的 MySQL,我是 MSSQL 用户......
Apologies if this isn't 100% legal MySQL, I'm a MSSQL user...
这将删除这两个条目。如果你想保留它们,你必须添加一个像 Will A 已经提议的 where 语句,但我建议你使用 >而不是 <保留较小的 user1 id。只是看起来更好:)
This deletes both entries. If you like to keep on of them you have to add a where statement like Will A alread proposed, but i suggest you to use > instead of < to keep the smaller user1 id. Just looks better :)