dongshou2024 2015-03-24 05:27
浏览 81

从mysql中的动态表列表中获取数据

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入