合并来自不同表的两列

时间:2015-01-10 12:56:18

标签: sql oracle

我有一个表“教师”,其中一列名为“电子邮件”,另一个表“学生”的列也称为“电子邮件”。

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

2 个答案:

答案 0 :(得分:1)

使用 union

select email from teachers
union
select email from students

答案 1 :(得分:1)

使用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