从多个列获取不同的值(包括联接)
这个把我难住了。
快速概述
我需要能够获取参与消息线程的人员的所有电子邮件地址。人们可以通过几种不同的方式将消息添加到此消息线程,并且由于人们可以是消息的发件人或接收消息,因此电子邮件地址可能位于不同的列中。
这是一个示例表。
messagesTable
ID | threadKey | fromEmail | toEmail | sentBy | sentTo
1 | ASJHD2 | [email protected] | [email protected] | null | null
2 | ASJHD2 | [email protected] | | null | null
3 | ASJHD2 | | | 55 | 88
我还有我的 Users
表。
ID | username | email
55 | Ike | [email protected]
88 | Sam | [email protected]
现在是否可以使用 ASJHD2
获取参与消息的所有不同电子邮件。请记住,用户表上还需要有一个联接。
This one has stumped me.
A quick overview
I need to be able to get all email addresses of people who have participated in a message thread. People can add messages to this messageThread via a few different means, and because people can either be the sender or receive a message, the email addresses might be in different columns.
Here is an example table.
messagesTable
ID | threadKey | fromEmail | toEmail | sentBy | sentTo
1 | ASJHD2 | [email protected] | [email protected] | null | null
2 | ASJHD2 | [email protected] | | null | null
3 | ASJHD2 | | | 55 | 88
I also have my Users
table.
ID | username | email
55 | Ike | [email protected]
88 | Sam | [email protected]
Now is it possible to get all of the distinct emails that have participated in the message with a threadKey of ASJHD2
. Keeping in mind that there would also need to be a join on the users table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先尝试只获取电子邮件,例如
First try to get just emails like
您需要做的本质上是联接到用户表,但说明使用任一字段进行联接的条件。然后,您可以根据您要查找的电子邮件地址对结果进行分组。
What you need to do is essentially join to your users table but stating the condition to use either field for the join. Then you group the results based on the email addresses that you're looking for.
仅供有兴趣的人使用。我使用了 Max 答案并添加了加入。这是我最终得到的最终查询。
Just for anyone interested. I used Max answer and added the Join also. This is the final query that I ended up with.