如何在MySQL中使用UNION 10表?

时间:2016-11-07 03:42:47

标签: mysql sql

我必须使用union 10表执行许多查询,如下所示。我一直在努力寻找一种有效的方法将它从order0循环到order9。有没有人解决类似的问题?

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;

1 个答案:

答案 0 :(得分: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;