简体   繁体   English

合并来自不同表的两列

[英]Merge two columns from different tables

I have one table "teachers" with a column called 'email', and another table "students" with a column also called 'email'. 我有一个表“ teachers”,其列名为“ email”,而另一个表“ students”,其列也称为“ email”。

I need to show all the e-mails both of the students and the teachers in one single column. 我需要在同一列中显示学生和老师的所有电子邮件。 That is, one list of all the existing e-mails no matter the position of the owner. 即,无论所有者的位置如何,所有现有电子邮件的一个列表。

Use union : 使用union

select email from teachers
union
select email from students

Use a union : 使用union

select email
from   teachers
union
select email
from   students

It concatenates the two results, and shows the overall distinct values. 它连接了两个结果,并显示了总体上不同的值。 (In contrary to union all that can result in duplicate values since all row values are shown, not only the distinct values) (与并union all相反,由于显示了所有行值,因此不仅会导致不同的值,而且可能导致重复的值)

Just a little extra, if you do want to know the origin of the email address, you could do this: 只需增加一点,如果您确实想知道电子​​邮件地址的来源,则可以执行以下操作:

select 'teacher' origin
,      id
,      email
from   teachers
union
select 'student' origin
,      id
,      email
from   students

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM