2012-01-17 42 views
2

我有以下疑問:結合兩個查詢 - 如何?

查詢1:

Select * from T10,T11,T12,T13,T14 
where T10.C0 = T11.C0 and T11.C1 = T12.C0 
and T12.C1 = T13.C0 and T13.C1 = T14.C0; 

查詢2:

Select * from T20,T21,T22,T23,T24 
where T20.C0 = T21.C0 and T21.C1 = T22.C0 
and T22.C1 = T23.C0 and T23.C1 = T24.C0; 

我如何可以結合這2次的查詢,以顯示這些表的所有值?我想要加入T10.C1 = T20.C1

當試圖union我得到一個關於不具有相同的列數,這是真正的警告,這些表是不一樣的


工會

Select * from 
"ProductConfig","Board","PcbBuild","Model","TcssCalib" 
where "Model"."idModel" = "PcbBuild"."Model" and "Board"."PcbBuild" = "PcbBuild"."idPcbBuild" 
and "Board"."idBoard" = "TcssCalib"."Board" and "ProductConfig"."TcssCalib" = "TcssCalib"."idTcssCalib" 

union 

Select * from"ProductBuild","TxResultsLink","TxResults","DspValues" where "ProductBuild"."idProductBuild" 
= "TxResultsLink"."ProductBuild" and "TxResults"."idTxResults" = "TxResultsLink"."TxResults" 
and "TxResults"."DspValues" = "DspValues"."idDspValues"; 

在這裏,我想ProductBuild.Productconfig加入ProductConfig.idProductConfig

給出錯誤:

[錯誤]錯誤:每個UNION查詢必須有相同數量的列


的當我嘗試inner join我在或接近inner

出現語法錯誤有沒有辦法加入這些2查詢在一起?

+2

A JOIN是要走的路。發佈您的JOIN查詢代碼,以便我們可以幫助調試它。另外,我希望這些是混淆的列名稱。 – 2012-01-17 13:47:43

+0

是的,這些是'組成'表和列的名稱(otherwize他們會變得不可讀,我認爲) – Moonlight 2012-01-17 13:49:14

回答

4

這是你想要什麼:

Select * from T10,T11,T12,T13,T14,T20,T21,T22,T23,T24 
where T10.C0 = T11.C0 and T11.C1 = T12.C0 
and T12.C1 = T13.C0 and T13.C1 = T14.C0 
and T10.C1 = T20.C1 and T20.C0 = T21.C0 and T21.C1 = T22.C0 
and T22.C1 = T23.C0 and T23.C1 = T24.C0; 

,以及另一種更易讀的方式把它,是這樣的:

select * from T10 
    inner join T11 on T10.C0 = T11.C0 
    inner join T12 on T11.C1 = T12.C0 
    inner join T13 on T12.C1 = T13.C0 
    inner join T14 on T13.C1 = T14.C0 
    inner join T20 on T10.C1 = T20.C1 
    inner join T21 on T20.C0 = T21.C0 
    inner join T22 on T21.C1 = T22.C0 
    inner join T23 on T22.C1 = T23.C0 
    inner join T24 on T23.C1 = T24.C0; 

對於UNION導致的表中的列數必須相同,並且來自相同/可轉換的數據類型。

Select * from T10,T11,T12,T13,T14 
where T10.C0 = T11.C0 and T11.C1 = T12.C0 
and T12.C1 = T13.C0 and T13.C1 = T14.C0 

UNION 

Select * from T20,T21,T22,T23,T24 
where T20.C0 = T21.C0 and T21.C1 = T22.C0 
and T22.C1 = T23.C0 and T23.C1 = T24.C0; 

Select * from 
"ProductConfig","Board","PcbBuild","Model","TcssCalib","ProductBuild","TxResultsLink","TxResults","DspValues" 
where "Model"."idModel" = "PcbBuild"."Model" and "Board"."PcbBuild" = "PcbBuild"."idPcbBuild" 
and "Board"."idBoard" = "TcssCalib"."Board" and "ProductConfig"."TcssCalib" = "TcssCalib"."idTcssCalib" and "ProductBuild"."idProductBuild" 
= "TxResultsLink"."ProductBuild" and "TxResults"."idTxResults" = "TxResultsLink"."TxResults" 
and "TxResults"."DspValues" = "DspValues"."idDspValues" 
and "ProductBuild"."ProductConfig" = "ProductConfig"."idProductConfig"; 
+0

這是我想要的,但我有我的2查詢已組成(由C#構建),我想知道如果我可以很容易加入這些查詢的 – Moonlight 2012-01-17 13:52:29

+0

正確的方法是使用我指定的查詢。 – 2012-01-17 13:55:28

+0

我嘗試了你的聯合方法,但是在我的情況下給出了一個錯誤(請參閱我編輯的問題) – Moonlight 2012-01-17 14:00:24