mysql查询中的“排序依据”在以后在php中回显时不起作用?
这是我的代码:mysql查询中的“排序依据”在以后在php中回显时不起作用?,php,mysql,Php,Mysql,这是我的代码: while ($shop = mysql_fetch_array($shop_result)) { $item_result = mysql_query("SELECT * FROM db_".$shop['ItemCategory']."s WHERE ItemId = '".$shop['ItemId']."' ORDER BY Level"); $item = mysql_fetch_assoc($item_result); echo("<item
while ($shop = mysql_fetch_array($shop_result)) {
$item_result = mysql_query("SELECT * FROM db_".$shop['ItemCategory']."s WHERE ItemId = '".$shop['ItemId']."' ORDER BY Level");
$item = mysql_fetch_assoc($item_result);
echo("<item>");
echo("<Element>" . $item['ItemElement'] . "</Element>");
echo("<ItemLevel>" . $item['Level'] . "</ItemLevel>");
echo("</item>");
我真的搞不懂。当我在Sequel Pro中运行相同的东西时,我得到了我想要的东西,但它是GUI。级别看起来是一个字符串。因此,它是作为字符串而不是数字排序的
以下是两个修复程序:首先,以数字形式订购:
order by Level + 0
第二,先按长度排序:
order by length(Level), Level
您并不是从$item_result中获取所有行,而是获取第一行。你需要另一个while循环
检查该特定条目的值,可能您有空格、隐藏字符或导致行先排序的内容。检查直接在mysql中进行查询时显示的顺序。这肯定是因为项目来自不同的循环,而您只从$item\u结果中获取一行。循环只针对$shop_result,因此排序基于该查询。我也想到了这一点,但在示例中,顺序应该是15、17、2、24,但假设级别15是首先显示的级别,最后是17。另一个选项是:按级别排序*1按CASTlevel排序,因为DECIMALLevel在我的数据库中是INT。出于某种原因,它可能仍然是一个字符串吗?
order by Level + 0
order by length(Level), Level
while ($shop = mysql_fetch_array($shop_result)) {
$item_result = mysql_query("SELECT * FROM db_".$shop['ItemCategory']."s WHERE ItemId = '".$shop['ItemId']."' ORDER BY Level");
while ($item = mysql_fetch_assoc($item_result)) {
echo("<item>");
echo("<Element>" . $item['ItemElement'] . "</Element>");
echo("<ItemLevel>" . $item['Level'] . "</ItemLevel>");
echo("</item>");
}
}