删除 MySQL 中的双向重复项

发布于 2024-09-15 12:05:27 字数 611 浏览 5 评论 0原文

我正在修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷清清 2024-09-22 12:05:27

抱歉,如果这不是 100% 合法的 MySQL,我是 MSSQL 用户......

DELETE F1
FROM friends F1
INNER JOIN friends F2
ON F2.user1 = F1.user2
AND F2.user2 = F1.user1
WHERE F1.user1 < F1.user2

Apologies if this isn't 100% legal MySQL, I'm a MSSQL user...

DELETE F1
FROM friends F1
INNER JOIN friends F2
ON F2.user1 = F1.user2
AND F2.user2 = F1.user1
WHERE F1.user1 < F1.user2
屋檐 2024-09-22 12:05:27
DELETE r
FROM friends l, friends r
WHERE l.user1 = r.user2
AND l.user2 = r.user1

这将删除这两个条目。如果你想保留它们,你必须添加一个像 Will A 已经提议的 where 语句,但我建议你使用 >而不是 <保留较小的 user1 id。只是看起来更好:)

DELETE r
FROM friends l, friends r
WHERE l.user1 = r.user2
AND l.user2 = r.user1

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文