加入两个不同的选择结果集

时间:2011-06-08 19:42:33

标签: sql plsql

我有两个看起来像这样的选择语句

SELECT DISTINCT(NAME, PHONE_NUMBER) FROM PHONE_CALLS GROUP BY NAME, PHONE_NUMBER;

SELECT DISTINCT(NAME, PHONE_NUMBER) FROM MISSED_CALLS GROUP BY NAME, PHONE_NUMBER;

这是两个单独的表格。

我想在PHONE_CALLS phone_number上加入每个语句的结果集,结果看起来像

phone_calls.NAME phone_calls.PHONE_NUMBER missed_calls.NAME, missed_calls.PHONE_NUMBER 

......如果有比赛..这可能吗?谢谢,

1 个答案:

答案 0 :(得分:3)

select distinct pc.name, pc.phone_number, mc.name missed_name 
from phone_calls pc, missed_calls mc
where pc.phone_number = mc.phone_number

这将为您提供接听和未接来电的所有电话号码。如果您还需要那些只接听或未接听电话的人,请使用

select distinct 
  pc.name, 
  coalesce(pc.phone_number,mc.phone_number) phone_number, 
  mc.name missed_name 
from phone_calls pc
full outer join missed_calls mc on pc.phone_number = mc.phone_number

代替。