SQL ORDER BY FIELD()使用数组

时间:2014-11-12 11:37:05

标签: php sql

大家好,我只是想知道是否有办法将数组传递给

ORDER BY FIELD()

功能,所以我有类似

的东西
<?php

$array = (5, 8, 7, 10);
$query = "SELECT * FROM table ORDER BY FIELD(id,".$array.")";    

?>
这可能吗?我将如何实现这一目标?

1 个答案:

答案 0 :(得分:4)

您可以使用implode制作以逗号分隔的列表:

<?php

$array = array(5, 8, 7, 10);
$query = "SELECT * FROM table ORDER BY FIELD(id,".implode( $array, ',' ).")";    

echo( $query );

?>

输出:

 SELECT * FROM table ORDER BY FIELD(id,5,8,7,10)