我有包括ORDER和LIMIT到SQL查詢中的問題。ORDER,LIMIT和使用sprintf查詢
$STH_1 = $DBH_R->query("SELECT table_name
FROM information_schema.tables
WHERE table_name
LIKE 'v_c_ei\_9%'
");
$stmts_1 = array();
//echo "date = ". $date."<br>"; // $date is today date
$date_60 = date('Y-m-d', strtotime('-60 day', strtotime($date))); // $date_60 = today - 60 days
while (($row_1 = $STH_1 -> fetch_assoc()) !== null){
$table_name = $row_1['table_name'];
$stmts_1[] = sprintf("SELECT *
FROM $table_name
WHERE (date_time >= '$date_60') AND (ei_code = '1117')
");
}
// at this place I need help, I think. I have few data from every query but I want to reduce the number of solutions to 1 per table
$stmt_1 = implode("\nUNION\n", $stmts_1);
$stmt_1 .= "\nORDER BY date_time ASC";
$STH_5_2 = $DBH_R->query($stmt_1);
while (($row_5_2 = $STH_5_2 -> fetch_assoc()) !== null){
上面的腳本工作正常。但我想限制數據的數量(我只需要每個表中的最後一個)。我嘗試通過
ORDER BY date_time DESC LIMIT 0,1
在sprintf查詢但它不想工作。當我添加ORDER等。我有答案
Fatal error: Call to a member function fetch_assoc() on a non-object in "while (($row_5_2 ..."
任何人都可以幫助嗎?
當您通過sprintf查詢嘗試時,代碼的外觀如何?你檢查從MySQL錯誤? –
@MarkByers可能沒有什麼,因爲他似乎使用mysqli。試試'echo $ DBH_R->錯誤;' –