Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过在PHP/MySQL中未按预期工作来订购_Php_Mysql_Sql_Sql Order By_Union - Fatal编程技术网

通过在PHP/MySQL中未按预期工作来订购

通过在PHP/MySQL中未按预期工作来订购,php,mysql,sql,sql-order-by,union,Php,Mysql,Sql,Sql Order By,Union,我的代码: $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']."'

我的代码:

$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";
}
我希望输出是一个从最低到最高排序的数字列表,但我的输出是这样的:

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

你知道为什么按排序不能像我期望的那样工作吗?

你的代码选择类别,并逐一检查。 它为已排序的每个类别选择ItemLevel,但不能同时选择所有类别。 第一类为10、37、62 第二类:15、35、55、75、95、105 第三类:70 等等

因此,您应该将两个sql查询合并为一个,并对结果进行排序。
如果不可能,则应将ItemLevel存储到数组中,并在打印前对其进行排序。

$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";
}

代码的问题是在一个循环中运行多个查询。每个查询的结果都是有序的,但不是全局结果

一种解决方案是使用循环构建联合sql查询。您可以在循环外运行查询;UNION查询的ORDER BY子句全局应用于其结果。当然,这假设所有查询都返回相同的列(否则需要修改代码)

代码

$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";
}

这样做是因为一次选择一行。与其他行没有关系。您可以将其存储到数组中并让PHP对其进行排序……或者您也可以使用一个查询并使用一个
join
。您也不应该再使用
mysql\uu
函数。这可能会在将来停止工作,而且是不安全的。未来必须阅读@user3783243是三年前的2015年,当时PHP 7.0删除了这些函数。警告:不要使用在PHP 7中删除的过时接口。替换类和指南类有助于解释最佳实践。这里没有参数,这在代码中有严重的错误。转义任何和所有用户数据,特别是从
$\u POST
$\u GET
中转义。由于第二个查询每次都从不同的、动态命名的表中进行选择,因此无法合并查询。就我个人而言,我会将所有项目数据放入一个数组(而不仅仅是
ItemLevel
值)并使用
usort
ItemLevel
值进行排序。感谢您的帮助,当我运行SQL查询时,我得到了这个MySQL错误:“使用的SELECT语句具有不同的列数”。我想我需要在查询中添加“虚拟列”来解决这个问题this@JDoe:是所有联合查询应返回相同的列。。。添加虚拟列确实是一个解决方案。