[英]PHP - return mysql rows in same order as called
I am trying to use a SELECT query to select 3 rows, but I want them in the same order I call for them. 我正在尝试使用SELECT查询来选择3行,但我希望它们以与调用它们相同的顺序进行。 This is what I attempted to do: 这是我尝试做的:
$array = array("50", "23", "67");
$list = implode(",", $array);
foreach($db->query("SELECT * FROM champs WHERE id IN ($list) ORDER BY
DECODE(id, $array[0], 1, $array[1], 2, $array[2], 3)") as $row) {
//Do stuff
}
however, I get: 但是,我得到:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax
is there a way to ensure it is always returned in the order I call it? 有没有办法确保它总是按我叫的顺序返回?
decode()
is an Oracle function, not a MySQL function. decode()
是一个Oracle函数,不是MySQL函数。 You should use case
, which works in both databases: 您应该使用case
,在两个数据库中都可以使用:
SELECT *
FROM champs
WHERE id IN ($list)
ORDER BY (CASE WHEN id = $array[0] THEN 1
WHEN id = $array[1] THEN 2
WHEN id = $array[2] THEN 3
END);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.