PHP語句在MYSQL中查詢多張表 並顯示在頁面上
用PHP語句實現查詢多張表 表的欄位是相同的、表名不同。例如:
abc201202,abc201203,abc201204...表名按月建的表
我新手不知道怎麼一次遍曆多張表,提取MYSQL中的資料。
------解決思路----------------------
$table = array("abc201202","abc201203","abc201204");
$result =array();
foreach($table as $table_name) {
$sql = "select * from ".$table_name;
$rec =mysql_query($sql);
while($row=mysql_fetch_row($rec)) {
$result[] = $row;
}
}
------解決思路----------------------
$select = array();
$table = array("abc201202","abc201203","abc201204");
foreach($table as $v) {
$select[] = "(select * from $v)";
}
$sql = join(' UNION ', $select);
$res = mysql_query($sql);
------解決思路----------------------
$table = array("abc201202","abc201203","abc201204");
$result=array();
$count = count($table)-1;//減去一個數組下標
for ($i=0;$i<$count;$i++){
$sql = $sql." select * from '".$table[$i]."' union all";
}
$sql=$sql." select * from '".$table[$count]."'";
$rec =mysql_query($sql);
while($row=mysql_fetch_row($rec)) {
$result[] = $row;
}