一次查询多个数据库

我有自己的数据库中的每个WordPress实例。 对于更新,我需要查询所有活动插件,这些插件存储在表“wp_options”中,可以通过

WHERE option_name='active_plugins' 

我如何访问所有活动的插件设置(分布在多个数据库中)并将其输出到单个SQL结果中? 我知道database.tablename语法,但我怎么从那里继续与上述Where语句?

单个数据库中的请求将如下所示:

 SELECT option_value FROM `database`.`wp_options` WHERE option_name="active_plugins" 
 SELECT option_value FROM `database1`.`wp_options` WHERE option_name="active_plugins" UNION SELECT option_value FROM `database2`.`wp_options` WHERE option_name="active_plugins" 

Pentium10的解决scheme很好,但是它的缺点是你必须扩展每个模式的查询。 下面的解决scheme使用一个准备好的语句为您的MySQL服务器上的所有模式生成一个结果集, 这些模式具有wp_options表。 这应该对你更方便。

 DROP PROCEDURE IF EXISTS `MultipleSchemaQuery`; DELIMITER $$ CREATE PROCEDURE `MultipleSchemaQuery`() BEGIN declare scName varchar(250); declare q varchar(2000); DROP TABLE IF EXISTS ResultSet; create temporary table ResultSet ( option_value varchar(200) ); DROP TABLE IF EXISTS MySchemaNames; create temporary table MySchemaNames ( schemaName varchar(250) ); insert into MySchemaNames SELECT distinct TABLE_SCHEMA as SchemaName FROM `information_schema`.`TABLES` where TABLE_NAME = 'wp_options'; label1: LOOP set scName = (select schemaName from MySchemaNames limit 1); set @q = concat('select option_value from ', scName, '.wp_options where option_name=\'active_plugins\''); PREPARE stmt1 FROM @q; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; delete from MySchemaNames where schemaName = scName; IF ((select count(*) from MySchemaNames) > 0) THEN ITERATE label1; END IF; LEAVE label1; END LOOP label1; SELECT * FROM ResultSet; DROP TABLE IF EXISTS MySchemaNames; DROP TABLE IF EXISTS ResultSet; END $$ DELIMITER ; CALL MultipleSchemaQuery(); 

又一个使用过程,游标,联合全部和准备语句来查询多个数据库的例子。 不需要删除和删除权限:

 USE `my_db`; DROP PROCEDURE IF EXISTS `CountAll`; DELIMITER $$ CREATE PROCEDURE `CountAll`(IN tableName VARCHAR(255)) BEGIN DECLARE db_name VARCHAR(250); DECLARE exit_loop BOOLEAN; DECLARE union_query TEXT DEFAULT ''; DECLARE my_databases CURSOR FOR SELECT DISTINCT `table_schema` FROM `information_schema`.`tables` WHERE `table_schema` LIKE 'myprefix\_%' AND `table_name` = tableName; DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE; OPEN my_databases; get_database: LOOP FETCH my_databases INTO db_name; IF exit_loop THEN -- remove trailing UNION ALL statement SET union_query = TRIM(TRAILING ' UNION ALL ' FROM union_query); LEAVE get_database; END IF; SET union_query = concat(union_query, 'SELECT COUNT(*) AS qty FROM ', db_name, '.', tableName, ' UNION ALL '); END LOOP get_database; CLOSE my_databases; SET @final_query = concat('SELECT SUM(qty) FROM (', union_query, ') AS total;'); PREPARE stmt1 FROM @final_query; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; END$$ DELIMITER ; CALL CountAll('wp_options'); 

Gruber的答案效果很好,但它有一个语法错误—第10行末尾有一个虚假的逗号。下面是代码,修正了语法错误:

 DELIMITER $$ CREATE PROCEDURE `MultipleSchemaQuery`() BEGIN declare scName varchar(250); declare q varchar(2000); DROP TABLE IF EXISTS ResultSet; create temporary table ResultSet ( option_value varchar(200) ); DROP TABLE IF EXISTS MySchemaNames; create temporary table MySchemaNames ( schemaName varchar(250) ); insert into MySchemaNames SELECT distinct TABLE_SCHEMA as SchemaName FROM `information_schema`.`TABLES` where TABLE_NAME = 'wp_options'; label1: LOOP set scName = (select schemaName from MySchemaNames limit 1); set @q = concat('select option_value from ', scName, '.wp_options where option_name=\'active_plugins\''); PREPARE stmt1 FROM @q; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; delete from MySchemaNames where schemaName = scName; IF ((select count(*) from MySchemaNames) > 0) THEN ITERATE label1; END IF; LEAVE label1; END LOOP label1; SELECT * FROM ResultSet; DROP TABLE IF EXISTS MySchemaNames; DROP TABLE IF EXISTS ResultSet; END $$