我必须对联合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;
最佳答案
您可以在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;
关于mysql - 如何在MySQL中创建UNION 10表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40457506/