2017-01-23 38 views
-2

我有兩個表:加入兩個表並顯示它來自

ID, actor, description.... 
FK_ID, other_description... 

我現在將加入這兩個表,讓我有以下結果:

ID|actor|description|other_description 
1, Bud Spencer, >MainDescription<, null 
1, Bud Spencer, null, Other_Description1 
1, Bud Spencer, null, Other_Description2 
..... 

我怎樣才能寫我的查詢?

謝謝。

+1

您是否嘗試過使用'UNION'和'JOIN'? –

+1

您希望MainDescription只出現在每個ID的第一行,是否正確?在每個後續的行上它應該是空的?只顯示第二行之後的Other_Description? – ADyson

回答

1

如果你需要在同一行,您可以使用左邊的值加入的ID和fk_id

select a.ID, a.actor, a.description, b.other_description 
from table1 as a 
left join table2 as b on a.ID = b.FK_ID 

,如果你在同一列中所需要的不同的值,你可以使用union

select ID, actor, description 
from table1 
union 
select fk_ID, null, other_description 
from table2 

,如果你需要的所有值,你可以使用UNION ALL

0
SELECT A.ID A.actor A.description B.other_description 
FROM tableA A 
INNER JOIN tableB B ON 
A.ID = B.FK_ID 
0
SELECT a.ID, a.actor, a.description, b.other_description 
FROM DB1 a 
JOIN DB2 b 
on a.ID=b.FK_ID 
相關問題