SQL Server:使用左连接从两个表中选择一列作为单个列
我正在尝试构建一个选择查询,该查询本质上将左连接两个表并将每个表中的一列显示为单列。
表结构类似于:
表 a:
id, email
表 b:
id, tablea_id, email
我试图获取电子邮件和电子邮件的单列(理想情况下没有重复或空值)。
理想的结果是:
[email protected]
[email protected]
[email protected]
返回的电子邮件地址可能来自 a 或 b。
也许联合是最有效的,但我无法弄清楚如何根据第一个表的 id 在第二个表上进行联合。
在寻找解决方案时,也许我的措辞不好,但我找不到任何例子。
感谢您的帮助。
I am trying to build a select query that will essentially left join two tables and display one column from each table as a single column.
the table structure is similar to:
table a:
id, email
table b:
id, tablea_id, email
I am trying to get a single column of email and email (with no dupes or nulls ideally).
ideal results would be:
[email protected]
[email protected]
[email protected]
and the email address that is returned could be from either a or b.
Maybe a union is what would work best, but I not able to figure out how to do a union on the second table based on the id of the first table.
When searching for a solution, perhaps my wording is bad, but I can't find any examples.
thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来你想要这样的东西:
It seems that you want something on the lines of this:
如果您只需要任一表中的电子邮件地址的唯一列表,您可以执行以下操作:
:
如果您正在查找表 B 中的电子邮件地址以及表 A 中存在于表 B 中的电子邮件地址的唯一列表,那么您可以执行以下操作 根据您的评论,您需要表 A 中的所有行,并且只需要表 B 中存在于表 A 中的行:
If you simply want a unique list of email addresses from either table you can do:
If you are looking for a unique list of email addresses from Table B and those from Table A that exist in Table B, then you can do:
If, per your comments, you need all rows from Table A and only rows from Table B where they exist in Table A:
这个怎么样:
How about this:
这可以帮助:
This could help: