用PHP語句實現查詢多張表 表的欄位是相同的、表名不同。例如:
abc201202,abc201203,abc201204...表名按月建的表
我新手不知道怎麼一次遍曆多張表,提取MYSQL中的資料。
回複討論(解決方案)
用for迴圈把sql語句包含在裡面重複執行,不知道能不能行得通
多次取資料再進行處理吧
$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; }}
PHP code
$table = array("abc201202","abc201203","abc201204");
$result =array();
foreach($table as $table_name) {
$sql = "select * from ".$table_name;
$rec =mysql_query($sql);
……
你這樣取出來的資料 不能分頁啊?
PHP code
$table = array("abc201202","abc201203","abc201204");
$result =array();
foreach($table as $table_name) {
$sql = "select * from ".$table_name;
$rec =mysql_query($sql);
……
你這樣做可也遍曆資料 ,但不能分頁資料啊?
$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);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; }
PHP code
$table = array("abc201202","abc201203","abc201204");
$result=array();
$count = count($table);
for ($i=0;$i<$count;$i++){
$sql = $sql." select * from '".$table[$i]."' union all";
}
$s……
你的CODE我試了下,echo $sql;輸出是:select * from 'abc201202' union all select * from 'abc201203' union all select * from 'abc201204' union all select * from ''
我修改了下輸出變成了:select * from twebmailaction_201208 union all select * from twebmailaction_201209 union all select * from twebmailaction_201210 union all
我用echo $num = mysql_num_rows($sql); 沒有結果。
PHP code
$select = array();
$table = array("abc201202","abc201203","abc201204");
foreach($table as $v) {
$select[] = "(select * from $v)";
}
$sql = join(' UNION ', $select);
$res = mysql_query……
用你的CODE我試了一下可以查詢 幾張表的資料條數和是對的。
但不知道怎麼回事,顯示資料架構有,分頁好像是對的,就是沒有資料庫欄位內容,
還有就是點擊下一頁後,提示:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\www_local\G03\list5.php on line 88 並且顯示資料的架構都沒有了。
新手求教育
$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; }
PHP code
$table = array("abc201202","abc201203","abc201204");
$result=array();
$count = count($table)-1;//減去一個數組下標
for ($i=0;$i<$count;$i++){
$sql = $sql." select * from '".$table[$i]."' unio……
現在可以可以了。還有點問題:
1、分頁好像還有點問題:$sql=$sql." select * from '".$table[$count]."'"." limit $page $pagesize";為了分頁我加了limit。這樣加對嗎?
2、遍曆迴圈我變了下,查詢顯示一個欄位的值:
while($row=mysql_fetch_row($rec)){
echo "
".$row[Username];
}
輸出就只有很多直線,沒有欄位的值?
limit $page $pagesize改成limit $page,$pagesize試試
limit $page $pagesize改成limit $page,$pagesize試試
不是這個問題。我前面有這個語句:$page.=',';
現在主要是顯示欄位內容不知道怎麼賦值?
PHP code
$select = array();
$table = array("abc201202","abc201203","abc201204");
foreach($table as $v) {
$select[] = "(select * from $v)";
}
$sql = join(' UNION ', $select);
$res = mysql_query……
就差指定欄位顯示的實現了!不知道怎麼遍曆指定欄位:例如while($row=(mysql_fetch_row($res))){
echo $row[user];
}分頁顯示user這個欄位
PHP code
$select = array();
$table = array("abc201202","abc201203","abc201204");
foreach($table as $v) {
$select[] = "(select * from $v)";
}
$sql = join(' UNION ', $select);
$res = mysql_query……
請問例如:while($row=(mysql_fetch_row($res))){
echo $row[user];
}像這樣分頁顯示user這個欄位怎麼沒有輸出
這樣不好的,還是用php處理吧