I have a list of tables from multiple database schemas.
$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'];
}
I have used these tables to create federated tables on a separate database
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'") ;
}
The federated tables have the same structure but with different names and data.These federated tables are expected to grow dynamically.To fetch data from the tables i can use union statement like:
mysqli_query($connection,"SELECT * FROM incentive_winners UNION SELECT * FROM d1_incentive_winners UNION SELECT * FROM d2_incentive_winners");
The above query is subsequently used to create a view. So, With the list of tables set to grow. How can i query the database without having to append another UNION?