0

我有八个数据库: order0,order2... order7,
它们都有表 'orders' 。
现在,我想要在所有八个数据库中执行 sql,例如 'select * from orders where order_id = 652454122'。
我需要像这样编写sql:

use order0
select * from orders where order_id = 652454122
use order1
select * from orders where order_id = 652454122
...
use order7
select * from orders where order_id = 652454122

需要复制七次,太可怕了!
有没有一些简单的方法,比如:

use order0/order1/order2... 
select * from orders where order_id = 652454122
4

2 回答 2

2

创建联合所有 8 个表的视图,例如:

CREATE VIEW vw_orders
AS
   SELECT *, 0 as DB FROM Order0..Orders
   UNION ALL
   SELECT *, 1 as DB FROM Order1..Orders
   UNION ALL
   ...
   SELECT *, 7 as DB FROM Order7..Orders

然后从中选择:

SELECT * FROM vw_orders WHERE order_id = 652454122
于 2013-10-08T09:30:43.397 回答
0

使用以下查询生成 8 个必需的查询 -

select CONCAT(" select * from ",table_schema,".orders 
where order_id = 652454122;") from information_schema.tables 
where table_schema like 'order%' and table_name='orders'

你不需要单独写8个查询,使用上面的查询,结果就是你需要写的8个查询。

于 2013-10-08T10:38:13.563 回答