MySQL 多重联接/子查询问题
大约一年来,我们一直允许用户使用不唯一的用户名和/或电子邮件地址登录(尽管每个用户都有唯一的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)