大家好,我只是想知道是否有办法将数组传递给
ORDER BY FIELD()
功能,所以我有类似
的东西<?php
$array = (5, 8, 7, 10);
$query = "SELECT * FROM table ORDER BY FIELD(id,".$array.")";
?>
这可能吗?我将如何实现这一目标?
答案 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)