我试图获取分配给特定子类别 (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