简体   繁体   English

ORDER BY 在 PHP/MySQL 中没有按预期工作

[英]ORDER BY not working as expected in PHP/MySQL

My code:我的代码:

$afer_result = mysql_query("SELECT * FROM aq_afers WHERE aferId = '".$afer_id."'");

while ($afer = mysql_fetch_array($afer_result)) {
$item_result = mysql_query("SELECT * FROM aq_".$afer['ItemCategory']."s WHERE ItemId = '".$afer['ItemId']."' ORDER BY ItemLevel");
$item = mysql_fetch_assoc($item_result);

echo $item['ItemLevel'];
echo "\n";
}

I expect the output to be a list of numbers to be sorted from lowest to highest, but this is what my output looks like:我希望输出是一个从最低到最高排序的数字列表,但这就是我的输出:

10
37
62
15
35
55
75
95
105
70
40
50
15
35
1
55

Any idea why ORDER BY is not working as I expect it to?知道为什么ORDER BY没有像我期望的那样工作吗?

The problem with your code is that you run several queries within a loop.您的代码的问题在于您在循环中运行多个查询。 The results of each query are ordered, but not the global results.每个查询的结果都是有序的,但不是全局结果。

A solution is to use the loop to build a UNION sql query.一种解决方案是使用循环来构建 UNION sql 查询。 The you can run the query outside the loop ;您可以在循环外运行查询; the ORDER BY clause of an UNION query applies globally to its results. UNION 查询的 ORDER BY 子句全局应用于其结果。 Of course this assumes that all queries return the same columns (else you'll need to adapt the code).当然,这假设所有查询都返回相同的列(否则您需要调整代码)。

Code ;代码 ;

$afer_result = mysql_query(
    "SELECT * 
    FROM aq_afers 
    WHERE aferId = '".$afer_id."'"
);

$sql_parts = array();
while ($afer = mysql_fetch_array($afer_result)) {
    array_push(
        $sql_parts,
        "SELECT * 
          FROM aq_".$afer['ItemCategory']."s
          WHERE ItemId = '".$afer['ItemId']."'"
    );
}

$sql = join(' UNION ALL ', $sql_parts);
$item_result = mysql_query($sql . 'ORDER BY ItemLevel');
while ($item = mysql_fetch_array($item_result)) {
    echo $item['ItemLevel'];
    echo "\n";
}

Your code select the categories and goes throug each of them.您的代码选择类别并遍历每个类别。 It selects the ItemLevel for each category sorted, but not all of them at the same time.它为每个排序的类别选择 ItemLevel,但不是同时选择所有类别。 For the first category 10, 37, 62 For the second category: 15, 35, 55, 75, 95, 105 For the third category: 70 Etc.第一类 10, 37, 62 第二类:15, 35, 55, 75, 95, 105 第三类:70 等

So you should merge your 2 sql queries into one and order that result.因此,您应该将 2 个 sql 查询合并为一个并对该结果进行排序。
If it is not possible you should store the itemlevels into an array and sort it before printing them out.如果不可能,您应该将 itemlevels 存储到一个数组中并在打印它们之前对其进行排序。

$afer_result = mysql_query("SELECT * FROM aq_afers WHERE aferId = '".$afer_id."'");
$itemLevels = [];
while ($afer = mysql_fetch_array($afer_result)) {
    $item_result = mysql_query("SELECT * FROM aq_".$afer['ItemCategory']."s WHERE ItemId = '".$afer['ItemId']."' ORDER BY ItemLevel");
    $item = mysql_fetch_assoc($item_result);
    $itemLevels[] = $item['ItemLevel'];
}
asort($itemLevels);
foreach ($itemLevels as $itemLevel) {
    echo $itemLevel;
    echo "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM