2010-04-15 93 views
-1

從三個表中選擇記錄並且這些表之間沒有關係的SQL查詢。其實我想讓它成爲一個視圖。SQL查詢從三個表中分別選擇記錄

假設有三個故事Table1, Table2, Table3

我想說明表1的記錄先用一些過濾條件 ,然後將記錄從表2 和去年從表3,當我們執行它表明類似的記錄的觀點像桌子一樣。

可以有任意數量的行,但記錄必須按此順序排列。

+0

什麼是三個表的結構? – CResults 2010-04-15 13:47:17

+0

是否有某些原因,您不能僅僅進行3個單獨的查詢並根據需要將它們呈現給用戶/視圖? – ewall 2010-04-15 13:48:14

+0

其實列數不一樣。 – Azhar 2010-04-15 13:58:26

回答

3

如果你想從每個o的所有記錄,我會建議使用UNION ALL而不是聯合桌子。 UNION將使用獨特的過濾出重複項。如果你不需要這只是減慢查詢速度。

進一步的解釋在這裏: http://wiki.lessthandot.com/index.php/Union_All

要告訴你如何處理時,你不必在每個表中的所有列:

select 
    1 as seq,col1, col2, col3, cast(null as varchar (40)) as col4 
    FROM Table1 
    where ... 
UNION ALL 
select 
    2 as seq,'Unknown', col2, null, col4 
    FROM Table2 
    where ... 
UNION ALL 
select 
    3 as seq ,col1, col2, col3, cast(null as varchar (40)) as col4 
    FROM Table3 
    where ... 
ORDER BY seq 
+1

但是不能有重複,因爲必須有某種指示排序順序的新列。因此,從第二個或第三個選擇匹配記錄中獲得記錄是不可能的。 – MJB 2010-04-15 13:51:11

+0

這就是爲什麼UNION ALL會更快,因爲你不會讓系統尋找你沒有的副本。 – HLGEM 2010-04-15 13:59:51

0

你可以做這樣的事情 - WHERE ID = 34僅僅是一個樣品過濾器:

create view vAllRecords as 
select 1 as Rank, Field1, Field2 from Table1 where ID = 34 
UNION 
select 2 as Rank, Field1, Field2 from Table2 
UNION 
select 3 as Rank, Field1, Field2 from Table3 

採用UNION將刪除任何重複。如果您知道不會有重複項,或者您想查看它們,則查詢將以UNION ALL代替運行得更快。

ORDER BY不容許的意見,所以你需要通過排名,當你從視圖中選擇命令:

select * 
from vAllRecords 
order by Rank 
1

你可以使用UNION查詢:

SELECT Field1, Field2, Field3, '1' as Sequence FROM Table1 WHERE SomeCriteria 
UNION 
SELECT Field7, Field5, Field6, '2' FROM Table2 WHERE SomeCriteria 
UNION 
SELECT Field4, Field8, Field9, '3' FROM Table3 WHERE SomeCriteria 
2

嘗試:

select 
    1,col1, col2, col3 
    FROM Table1 
    where ... 
UNION ALL 
select 
    2,col1, col2, col3 
    FROM Table2 
    where ... 
UNION ALL 
select 
    3,col1, col2, col3 
    FROM Table3 
    where ... 
ORDER BY 1 

請注意,三個查詢中的每一個都需要有相同數量的柱ns並且數據類型也應該一致。此外,我使用UNION ALL來加快查詢速度,因爲沒有用途消除三個查詢之間的重複項,因爲順序表將保證沒有任何問題。

不具有在結果集中嘗試順序列:

SELECT 
    col1,col2,col3 
    FROM (select 
      1 as seq,col1, col2, col3 
      FROM Table1 
      where ... 
      UNION ALL 
      select 
      2 as seq,col1, col2, col3 
      FROM Table2 
      where ... 
      UNION ALL 
      select 
      3 as seq,col1, col2, col3 
      FROM Table3 
      where ... 
     ) dt 
    ORDER BY seq 
1

如何:

create view AZ_VIEW as 
select 1 as orderby, tbl1Col1 as col1, tbl1Col2 as col2, tbl1col3 as col3 from Table1 where criteria1='val' 
union 
select 2, tbl2Col1, tbl2Col2, tbl2col3 from Table2 where criteria2='anotherval' 
union 
select 3, tbl3Col1, tbl3Col2, tbl3col3 from Table3 where criteria3='athirdval' 
; 
1

如果你的表共享相同的列,您可以使用Union All

Select col1, col2, 1 As seq 
From table1 
Union All 
Select col1, col2, 2 As seq 
From table1 
Union All 
Select col1, col2, 3 As seq 
From table1 
Order By seq 
1

你可以聯盟的三個表,照顧以確保他們都返回相同數量的字段。有一個簡單的作弊控制順序(參見下面):

SELECT * FROM ( 選擇A,B,C,1作爲ListOrder FROM表1 UNION 選擇A,B,C,2作爲ListOrder FROM表2 UNION 選擇A,b,C,3爲ListOrder FROM表3 ) ORDER BY ListOrder