2014-01-12 26 views
-1

我有一個ID的數組,它們在每行的表的上下文中是唯一的。我想這樣做是爲了查詢數據庫並獲得通過插入日期排序(它已經在表中的字段),但只有那些表哪個ID是在陣列中查詢某些行並按日期排序

$query = SELECT this,andthis,andthis,andthis FROM table WHERE id=(inside array of ids)? ORDER BY date_of_insertion;

的結果是fetch_assoc(),因此獲得一個關聯數組。我真的想會是這樣的最終數組中:

[1] ID,這個,andthis,andthis,andthis

[2] ID,這個,andthis,andthis,andthis

[3] ...

甚至如果表中有其他ID和行,只是沒有質疑,因爲他們指定數組中則沒有。那些被查詢的應該按時間排序。有關如何在此方面實現最佳性能的任何想法?

回答

0

你可以試試這個:

// this is the array of ids 
$array_of_ids = array(); 

// joins the ids in the array into a string, separating each id with a comma 
$ids = implode(',', $array_of_ids); 

$query = "SELECT this, andthis, andthis, andthis FROM table WHERE id IN (" . $ids . ") ORDER BY date_of_insertion"; 
+0

是啊,很好地想!我會嘗試一下,並讓你知道它是否成功。 – user111671