2016-11-07 41 views
0

我必須使用union 10表執行很多查詢,如下所示。我一直在努力尋找一種有效的方式將它從order0循環到order9。有沒有人解決類似的問題?如何在MySQL中聯合10個表?

select col1, col2 from order0 union all 
select col1, col2 from order1 union all 
select col1, col2 from order2 union all 
select col1, col2 from order3 union all 
select col1, col2 from order4 union all 
select col1, col2 from order5 union all 
select col1, col2 from order6 union all 
select col1, col2 from order7 union all 
select col1, col2 from order8 union all 
select col1, col2 from order9; 
+0

您的查詢應該沒問題。你的屁股是什麼? –

+0

你想在這個查詢中實現什麼?在你的帖子 – Beginner

+1

中說明你似乎有錯誤的數據庫設計。它聞起來像oder0到order10應該真的是一個表 – e4c5

回答

0

您可以使用UNION語句創建視圖。這樣,您只需使用視圖名稱即可查看查詢中的所有訂單(例如,從訂單中選擇*)。

CREATE VIEW orders 
AS 
select col1, col2 from order0 union all 
select col1, col2 from order1 union all 
select col1, col2 from order2 union all 
select col1, col2 from order3 union all 
select col1, col2 from order4 union all 
select col1, col2 from order5 union all 
select col1, col2 from order6 union all 
select col1, col2 from order7 union all 
select col1, col2 from order8 union all 
select col1, col2 from order9;