2015-06-24 84 views
0

下面是代碼(模擬全外連接sqlite3的):選擇列在聯盟兩個表與一個重疊列名sqlite的

SELECT EMP_ID, ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID 
union 
SELECT EMP_ID, ID, NAME, DEPT FROM department LEFT OUTER JOIN company ON COMPANY.ID = DEPARTMENT.EMP_ID 

公司表具有列:ID,姓名,年齡,地址, ,工資。 部門表具有列:id,dept,emp_id。

問題是兩個表都有id列,但我只想從公司選擇id列。我怎樣才能做到這一點?

+0

在部門選擇中用'null'替換'id'? –

回答

1

通過使用別名解決:

SELECT EMP_ID, c.id, NAME, DEPT FROM COMPANY as c LEFT OUTER JOIN DEPARTMENT as d ON c.ID = d.EMP_ID 
union 
SELECT EMP_ID, c.id, NAME, DEPT FROM department as d LEFT OUTER JOIN company as c ON c.ID = d.EMP_ID; 

這很好地說明了一個完整的外部連接的結果:

3 Teddy 
    4 Mark  
    5 David 
    6 Kim 
    7 James 
    8 Kitos 
    9 Paul  
    10 James 
    11 James 
    12 James 
    13 James 
    14 James 
    15 James 
    16 James 
    17 James 
1 1 Paul IT Billing 
2 2 Allen Engineering 
77    Finance 

空白單元爲空。