Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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 “SQL”;“在哪里?”;返回自动排序的结果_Php_Mysql_Sql_Where - Fatal编程技术网

Php “SQL”;“在哪里?”;返回自动排序的结果

Php “SQL”;“在哪里?”;返回自动排序的结果,php,mysql,sql,where,Php,Mysql,Sql,Where,当我使用WHERE IN语句运行查询时,它似乎会自动对返回的产品id进行排序 $SQL = "SELECT * FROM PIM WHERE product_id in (10,8,1,3)"; foreach($conn->query($sql) as $row) { echo $row['product_id'] . "<br>"; } 我希望它们按照输入的顺序返回(10,8,1,3)因为在原始查询中,您没有指定MySQL应该使用的顺序,然后使用ASC,请尝试使用o

当我使用
WHERE IN
语句运行查询时,它似乎会自动对返回的产品id进行排序

$SQL = "SELECT * FROM PIM WHERE product_id in (10,8,1,3)";

foreach($conn->query($sql) as $row) {
  echo $row['product_id'] . "<br>";
}

我希望它们按照输入的顺序返回(10,8,1,3)

因为在原始查询中,您没有指定MySQL应该使用的顺序,然后使用ASC,请尝试使用order BY FIELD(),如下所示:

SELECT * FROM PIM WHERE product_id in (10,8,1,3) ORDER BY FIELD(product_id, 10,8,1,3);

检查此项。

因为在原始查询中,您没有指定MySQL应使用的顺序,然后正在使用ASC,请尝试使用order BY FIELD(),如下所示:

SELECT * FROM PIM WHERE product_id in (10,8,1,3) ORDER BY FIELD(product_id, 10,8,1,3);
检查此项。

尝试:

select * from PIM where id in (1,3,8,10) order by find_in_set(id,'10,8,1,3');
尝试:


它可能会按顺序将它们退回;但是,除非您指定ORDER BY子句,否则返回顺序是不确定的(
ORDER BY字段(product_id,10,8,1,3)
),如果您希望SQL结果按特定顺序,则必须添加特定的
ORDER BY
子句,因为它可能会按该顺序返回它们;但是,除非您指定ORDER BY子句,否则退货订单是不确定的(
ORDER BY字段(product_id,10,8,1,3)
),如果您希望SQL结果按特定顺序排列,则必须添加特定的
ORDER BY
clauseThanks,有效:-)谢谢,有效:-)