2011-09-15 47 views
0

我已經如圖三個表(debdoschar)和我正在尋找一個SQL查詢來獲取result表如下:如何將多個表連接成一個MySQL?

table: deb 
+----+-------------+--------+ 
| id | label  | dos_id | 
+----+-------------+--------+ 
| 1 | right  |  1 | 
| 2 | left  |  2 | 
+----+-------------+--------+ 

table:char 
+----+-------------+--------+ 
| id | name  | dos_id | 
+----+-------------+--------+ 
| 1 | jack  |  1 | 
| 2 | thaw  |  1 | 
| 3 | lbaz  |  2 | 
| 4 | amar  |  3 | 
+----+-------------+--------+ 

table:dos 
+----+-------------+ 
| id | color  | 
+----+-------------+ 
| 1 | black  | 
| 2 | white  | 
+----+-------------+ 


result table 
+----+-------------+--------+ 
| id | color  | value | 
+----+-------------+--------+ 
| 1 | black  | right | 
| 1 | black  | jack | 
| 1 | black  | thaw | 
| 2 | white  | left | 
| 2 | white  | lbaz | 
| 2 | white  | amar | 
+----+-------------+--------+ 

我試圖接合左右,但它沒」工作。

我在找的是每個dos行我得到相應的chardeb其他列中的值。

也許在一個行中選擇查詢可能會有所幫助。我不知道。

請幫助我,謝謝。

回答

1

您需要運行2個單獨的查詢並將結果組合到一個結果集中。爲此,您可以使用UNION運算符是這樣的:

SELECT * FROM 
(
    SELECT dos.id id, dos.color color, deb.label value 
    FROM dos INNER JOIN deb on dos.id = deb.dos_id 
    UNION 
    SELECT dos.id id, dos.color color, char.name value 
    FROM dos INNER JOIN char on dos.id = char.dos_id 
) 
ORDER BY id 

編輯:

SELECT * 
FROM 
( 
    SELECT dos.id AS id, dos.date_facture AS facture, deb.libelle AS lbl, '' AS nom 
    AS lbl FROM jos_tktransit_dossier dos INNER JOIN jos_tktransit_deboure deb ON dos.id = deb.id_dossier 
    UNION 
    SELECT dos.id AS id, dos.date_facture AS facture, '' AS lbl, charg.nom_charge AS nom 
    FROM jos_tktransit_dossier dos INNER JOIN jos_tktransit_charge_assistance charg ON dos.id = charg.id_dossier 
) 
+0

我得到這個 「1248 - 每一個派生表必須有其自己的別名」 ,這是我的查詢 SELECT * FROM ( 選擇dos.id爲ID,dos.date_facture AS製作,deb.libelle AS LBL FROM jos_tktransit_dossier和DOS INNER JOIN jos_tktransit_deboure AS DEB ON dos.id = DEB .id_dossier UNION 選擇dos.id爲ID,dos.date_facture AS製作,charg.nom_charge AS NOM FROM jos_tktransit_dossier和DOS INNER JOIN jos_tktransit_charge_assistance AS臨時代辦ON dos.id = charg.id_dossier ) –

+0

這不會工作,因爲你有不同的領域兩個查詢中的名稱。他們必須是一樣的。在我的例子中,兩個查詢都返回(id,color,value),但是註釋中的查詢返回(id,facture,lbl)和(id,facture,nom)。 lbl!= nom,所以UNION不起作用。 – StevieG

+0

謝謝,但有沒有其他解決方案 –

0

也許是這樣的:

SELECT dos.id, color, deb.label as value FROM dos, deb WHERE dos_id = dos.id 
UNION SELECT dos.id, color, char.name as value FROM dos, char WHERE dos_id = dos.id; 
+0

我得到這個 「1248 - 每一個派生表必須有它自己的別名」 ,這是我的查詢 SELECT * FROM ( 選擇dos.id爲ID,dos.date_facture AS製作,deb.libelle AS LBL FROM jos_tktransit_dossier AS dos INNER JOIN jos_tktransit_deboure AS deb ON dos.id = deb.id_dossier UNION SELECT dos.id AS id,dos。date_facture AS票據charg.nom_charge名稱 從AS jos_tktransit_dossier背面jos_tktransit_charge_assistance INNER JOIN AS ON加載dos.id = charg.id_dossier ) –

0

嘗試以下操作:

(select d.id,d.color,c.name from dos d , char1 c where d.id=c.dos_id) union (select d.id,d.color,e.label from dos d, deb e where d.id= e.dos_id)

這應該輸出如下:

ID COLOR NAME 
1 black jack 
1 black right 
1 black thaw 
2 white lbaz 
2 white left 

...但它並不完全正確的順序顯示值:

+----+-------------+--------+ 
| id | color  | value | 
+----+-------------+--------+ 
| 1 | black  | right | 
| 1 | black  | jack | 
| 1 | black  | thaw | 
| 2 | white  | left | 
| 2 | white  | lbaz | 
| *2 | white  | amar* | 
+----+-------------+--------+