我有八个数据库:order0,order2 ... order7,
他们都有桌子'订单'
现在,我希望所有八个数据库中的exec sql都像'select * from orders where order_id = 652454122'。
我需要编写像:
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
答案 0 :(得分: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
答案 1 :(得分: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个查询。