3

SQL:

SELECT question,alt_1,alt_2,alt_3,alt_4 FROM questions WHERE id IN ('12','2','32','23')

PHP:

while ($questions = mysql_fetch_array($game)){
    echo "$questions[question]";
}

我希望按照将值发送到 sql 的顺序打印行:12 - 2 - 32 - 23。

但是 fetch_array/sql(?) 似乎对值进行了排序,因此顺序如下:2 - 12 - 23 - 32 (从最小的数字开始......)

有什么办法可以阻止数组按数字排序!?

4

2 回答 2

0

试试看,
“ORDER BY FIELD(id'12','2','32','23')”

          SQL: "SELECT question,alt_1,alt_2,alt_3,alt_4 FROM questions WHERE id IN 
          ('12','2','32','23') ORDER BY FIELD(id'12','2','32','23')"

           while($questions = mysql_fetch_array($game)){
           echo "$questions[question]";
       }
于 2013-02-02T09:56:18.783 回答
0

使用FIND_IN_SET

SELECT question,alt_1,alt_2,alt_3,alt_4 
FROM questions 
WHERE id IN ('12','2','32','23') 
ORDER BY FIND_IN_SET(id, '12,2,32,23')

注意FIND_IN_SET(id, '12,2,32,23') 12,2 没有空格

FIND_IN_SET(field, 'val1,val2')
于 2013-02-02T10:07:46.943 回答