[英]Merge two columns from different tables
我有一個表“ teachers”,其列名為“ email”,而另一個表“ students”,其列也稱為“ email”。
我需要在同一列中顯示學生和老師的所有電子郵件。 即,無論所有者的位置如何,所有現有電子郵件的一個列表。
使用union :
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.