0

我在下面有和排列

  $titles=array("Dr.","Ms.","Mr.");

在 foreach 循环之后,我创建了下面的查询

SELECT * FROM `table` WHERE title = 'Dr.' or title = 'Ms.' or title = 'Mr.' ) group by sentences

输出开始于

先生 女士 先生 博士

我想把博士。首先是数组中的女士而不是先生

4

2 回答 2

4

只要您的输入经过净化,您就可以使用它implode来准备语句。

$stmt = 'SELECT * FROM `table` ';
$stmt .= "WHERE title = '" . implode("' OR title = '", $titles) . "'";

结果

SELECT * FROM `table` WHERE title = 'Dr.' OR title = 'Ms.' OR title = 'Mr.'

查看演示

您也可以使用IN

$stmt = 'SELECT * FROM `table` ';
$stmt .= "WHERE title IN ('" . implode("', '", $titles) . "')";

结果

SELECT * FROM `table` WHERE title IN ('Dr.', 'Ms.', 'Mr.')

你需要修复你的GROUP BY; 你没有正确使用它。

如果要对标题的顺序进行排序,可以尝试:

ORDER BY
  CASE
    WHEN title = 'Dr' THEN 1
    WHEN title = 'Ms.' THEN 2
    WHEN title = 'Mr.' THEN 3
    ELSE 4
  END
于 2013-05-08T15:49:45.117 回答
0

you can try in statement which is easy to read.

SELECT * 
FROM  `urls` 
WHERE  `id` 
IN ( 1, 2, 4 ) 
ORDER BY  `urls`.`id` ASC 
LIMIT 0 , 30
于 2013-05-08T15:57:58.167 回答