合并来自不同表的两列

Merge two columns from different tables

我有一个 table "teachers" 的列名为 'email',另一个 table "students" 的列也称为 'email' .

我需要在一个列中显示学生和老师的所有电子邮件。也就是说,所有现有电子邮件的一个列表,无论所有者的职位如何。

使用联合:

select email from teachers
union
select email from students

使用 union:

select email
from   teachers
union
select email
from   students

它连接两个结果,并显示整体不同的值。 (与可能导致重复值的 union all 相反,因为显示了所有行值,而不仅仅是不同的值)

补充一点,如果您想知道电子​​邮件地址的来源,可以这样做:

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