MySQL 多重联接/子查询问题

发布于 2024-08-16 07:16:56 字数 1135 浏览 7 评论 0原文

大约一年来,我们一直允许用户使用不唯一的用户名和/或电子邮件地址登录(尽管每个用户都有唯一的 ID)。尽管系统可以优雅地处理重复的用户名/电子邮件,但我们最终决定强制执行唯一的用户名和电子邮件地址。我的任务是在 MySQL 中生成一个表,该表将显示重复项以及使用重复项 ID 的表(即依赖于重复项用户 ID 的表,使用 1 表示 true,0 表示 false)。一旦重复数据被标记为删除,该表将被用作参考。简而言之,我希望生成一个类似这样的表:

| User_id | Username | Email | Exists_in_Table1 | Exists_in_Table2 | Exists_in_Table3 |
-----------------------------------------------------------------------------------------------------------
| 0001.....| test1.........| email.| 0..........................| 0..........................| 1..........................|
| 0002.....| test2.........| email.| 0..........................| 1..........................| 1..........................|
| 0003.....| test3.........| email.| 1..........................| 1..........................| 1..........................|


如何实现这一点并不重要。由于我的 SQL 技能有些欠缺,我打算使用 PHP 和一些简单的 SQL 查询以编程方式完成此操作。然而,我相信单个 SQL 查询或一系列查询(不使用 PHP)是最简洁的方法。我知道如何查询重复项,但我似乎无法弄清楚如何查询多个表并通过用户 ID 以适当的方式连接它们。我感谢所有对此的帮助。谢谢。

For about a year now, we’ve been allowing our users to login with usernames and/or email addresses that are not unique (though each user does have a unique id). Although the system handles duplicate usernames/emails elegantly, we’ve decided to finally enforce unique usernames and email addresses. I’ve been tasked with generating a table in MySQL that will show the duplicates and the tables in which a duplicate’s id is being used (i.e. the tables dependent on the duplicate’s user id, using 1 for true and 0 for false). This table will then be used as a reference once duplicate data is marked for deletion. In short, I’m looking to generate a table something like this:

| User_id |
Username |
Email |
Exists_in_Table1 |
Exists_in_Table2 |
Exists_in_Table3 |

-----------------------------------------------------------------------------------------------------------

| 0001.....|
test1.........|
email.|
0..........................|
0..........................|
1..........................|

| 0002.....|
test2.........|
email.|
0..........................|
1..........................|
1..........................|

| 0003.....|
test3.........|
email.|
1..........................|
1..........................|
1..........................|

It doesn’t matter much how this is accomplished. Since my SQL skills are somewhat lacking, I intended to do this programmatically using PHP and a number of simple SQL queries. However, I believe a single SQL query or a series of queries (without the use of PHP) is the cleanest approach. I know how to query for duplicates, but I can’t seem to figure out how to query multiple tables and join them by the user id in an appropriate manner. I appreciate any and all help with this. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

山色无中 2024-08-23 07:16:56
SELECT u.User_id, u.Username, u.Email,
 IF(t1.User_id IS NULL, 0, 1) AS Exists_in_Table1,
 IF(t2.User_id IS NULL, 0, 1) AS Exists_in_Table2,
 IF(t3.User_id IS NULL, 0, 1) AS Exists_in_Table3
FROM Users u
LEFT OUTER JOIN Table1 t1 USING (User_id)
LEFT OUTER JOIN Table2 t2 USING (User_id)
LEFT OUTER JOIN Table3 t3 USING (User_id);
SELECT u.User_id, u.Username, u.Email,
 IF(t1.User_id IS NULL, 0, 1) AS Exists_in_Table1,
 IF(t2.User_id IS NULL, 0, 1) AS Exists_in_Table2,
 IF(t3.User_id IS NULL, 0, 1) AS Exists_in_Table3
FROM Users u
LEFT OUTER JOIN Table1 t1 USING (User_id)
LEFT OUTER JOIN Table2 t2 USING (User_id)
LEFT OUTER JOIN Table3 t3 USING (User_id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文