我試圖獲取分配給特定子類別 (id = 9)的配置文件名稱。當我運行下面的代碼時,我得到了我想要的配置文件,但對於某些 原因,foreach循環中的ORDER BY子句不按字母順序排列它們的名稱 。相反,它們的排列方式與「子類別」表中 「配置文件」字段中的順序相同(配置文件的ID以逗號分隔)。 例如,如果在子類別[「剖面」]我已經」,5,1,2' 輪廓名稱將按照以下順序顯示 :在MySQL中的順序子句不在foreach循環內工作
- 資料與ID = 5
- 資料ID爲= 1
- 資料與ID = 2
我使用的爆炸()函數來獲取「子類別」 表內的各輪廓的ID,然後使用該ID從檢索其信息'profile'表使用 foreach循環內的查詢。
我在這裏錯過了什麼嗎?謝謝你的幫助。
這裏是我的代碼:
<?php
$subcategories=mysql_query("select * from subcategories where id='9'");
while ($subcategories = mysql_fetch_array($subcategories))
{
$profiles = $subcategories['profiles'];
$profiles = explode(',', $profiles);
foreach ($profiles as $p)
{
$all_places = mysql_query("select * from profile where id='$p' and active='1' order by name asc");
while ($profile = mysql_fetch_array($all_places))
{
echo $profile['name'];
}
}
}
?>
如果沒有'ORDER BY'子句(如果被排除),它工作正常嗎? – Lion 2012-07-16 09:02:12
是的,它工作正常。 – 2012-07-16 09:26:52