我有一个来自多个数据库模式的表列表。
$result = mysql_query("SELECT DISTINCT TABLE_NAME,TABLE_SCHEMA FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN('incentives','winner') AND TABLE_SCHEMA!='federated'");
while ($row = mysql_fetch_array($result)) {
$databases[]=$row['TABLE_SCHEMA'];
$tables[]=$row['TABLE_NAME'];
}
我已经使用这些表在单独的数据库上创建联合表
foreach(array_keys($databases) as $key){
$db=$databases[$key];
$tb=$tables[$key];
$createserver=mysql_query("CREATE SERVER $db
FOREIGN DATA WRAPPER mysql
OPTIONS (USER 'root', HOST 'localhost', PORT 3306, DATABASE '$db')");
$insertWinners= mysqli_query($connection,"CREATE TABLE IF NOT EXISTS $tb (
`iid` int(11) NOT NULL,
`incentive` int(11) NOT NULL,
`winner` int(11) NOT NULL,
`mpesa` varchar(16) NOT NULL,
`ddate` date NOT NULL,
`stato` enum('0','1') NOT NULL DEFAULT '0',
`flag` enum('0','1') NOT NULL DEFAULT '0'
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='$db/$tb'") ;
}
联邦表具有相同的结构,但具有不同的名称和数据。这些联合表预计会动态增长。要从表中获取数据,我可以使用union语句,如:
mysqli_query($connection,"SELECT * FROM incentive_winners UNION SELECT * FROM d1_incentive_winners UNION SELECT * FROM d2_incentive_winners");
以上查询随后用于创建视图。 因此,随着表的列表设置增长。如何查询数据库而不必附加另一个UNION?