在dbase中我有幾個表,名字爲aaa_9xxx, aaa_9yyy, aaa_9zzz
。我想查找指定的所有數據DATE
,並用TIME ASC
進行顯示。按照ASC排序的幾個MySQL表的數據
首先,我必須找到與dBASE一個表:
$STH_1a = $DBH->query("SELECT table_name
FROM information_schema.tables
WHERE table_name
LIKE 'aaa\_9%'
");
foreach($STH_1a as $row)
{
$table_name_s1[] = $row['table_name'];
}
其次,我必須找到一個數據機智的具體日期,並TIME ASC
表現出來:
foreach($table_name_s1 as $table_name_1)
{
$STH_1a2 = $DBH->query("SELECT *
FROM `$table_name_1`
WHERE
date = '2011-11-11'
ORDER BY time ASC
");
while ($row = $STH_1a2->fetch(PDO::FETCH_ASSOC)) {
echo " ".$table_name_1."-".$row['time']."-".$row['ei_name']." <br>";
}
}
..但它顯示按表格名稱排序的數據,然後按TIME ASC
。我必須將所有這些數據(來自所有表)按TIME ASC
排序。
謝謝dev-null-dweller,Andrew Stubbs和Jaison Erick的幫助。 我測試埃裏克解決方案:
foreach($STH_1a as $row) {
$stmts[] = sprintf('SELECT *
FROM %s
WHERE date="%s"', $row['table_name'], '2011-11-11');
}
$stmt = implode("\nUNION\n", $stmts);
$stmt .= "\nORDER BY time ASC";
$STH_1a2 = $DBH->query($stmt);
while ($row_1a2 = $STH_1a2->fetch(PDO::FETCH_ASSOC)) {
echo " ".$row['table_name']."-".$row_1a2['time']."-".$row_1a2['ei_name']." <br>";
}
它的工作,但我有問題「table_name
」 - 它總是LAST
表名。
// -------------------------------------------- --------------------------
...和所有修補程序的結束解決方案,感謝所有的幫助,:))
foreach($STH_1a as $row) {
$stmts[] = sprintf("SELECT *, '%s' AS table_name
FROM %s
WHERE date='%s'", $row['table_name'], $row['table_name'], '2011-11- 11');
}
$stmt = implode("\nUNION\n", $stmts);
$stmt .= "\nORDER BY time ASC";
$STH_1a2 = $DBH->query($stmt);
while ($row_1a2 = $STH_1a2->fetch(PDO::FETCH_ASSOC)) {
echo " ".$row_1a2['table_name']."-".$row_1a2['time']."-".$row_1a2['ei_name']." <br>";
}
thnks,埃裏克,它的工作,但我有問題'table_name' - 信息上面。 – Andrew
上面的修復將返回你想要的。 –
Thnks,我的問題與您的修復。 Err顯示//致命錯誤:調用成員函數對非對象的fetch()//代碼while while($ row_1a2 = $ STH_1a2-> fetch(PDO :: FETCH_ASSOC)){ ' – Andrew