2013-05-12 53 views
0

我有三張桌子。 table1,table2和table3。 enter image description here我應該如何做以下連接以獲得所需的結果?

因此,屬性對象R1,並R2 table1中對應於表2 「雅虎」 的對象名稱, 「電子郵件」 和 「平」。並且R3對應於表3的u-id的名稱「Jake」。

現在我需要JOIN表1與表2的3倍,並與表3次,以獲得如下表:

enter image description here

我聽說,我需要使用LEFT JOIN,但不能得到所期望的結果。任何幫助將不勝感激。
此外,新表應通過只做一個(大)查詢來創建。

感謝

回答

1
SELECT a.ID, 
     e.name object, 
     a.date, 
     b.name R1, 
     c.name R2, 
     d.`u-name` R3 
FROM table1 a 
     INNER JOIN table2 b 
      ON a.r1 = b.object 
     INNER JOIN table2 c 
      ON a.r2 = c.object 
     INNER JOIN table2 e 
      ON a.object = e.object 
     INNER JOIN table3 d 
      ON a.r3 = d.`u-id` 

爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:

上面的查詢使用INNER JOINtable1如果合適的話所有字段爲不可爲空。當其中一個是可以爲空時並且您想要返回table1的所有列表,請使用LEFT JOIN而不是INNER JOIN


「..Also新表應該這樣做只有一個(大)查詢來創建。」

CREATE TABLE tableNameHere(col1 INT,....) -- list of columns 
AS 
SELECT a.ID, 
     e.name object, 
     a.date, 
     b.name R1, 
     c.name R2, 
     d.`u-name` R3 
FROM table1 a 
     INNER JOIN table2 b 
      ON a.r1 = b.object 
     INNER JOIN table2 c 
      ON a.r2 = c.object 
     INNER JOIN table2 e 
      ON a.object = e.object 
     INNER JOIN table3 d 
      ON a.r3 = d.`u-id` 
+1

你是不是缺少一個更加入?就像「物體」一樣? – Brian 2013-05-12 02:06:43

+0

你是對的。我會更新答案。 – 2013-05-12 02:11:36

+0

看到我更新的答案。 – 2013-05-12 02:12:59

相關問題