我在下面有和排列
$titles=array("Dr.","Ms.","Mr.");
在 foreach 循环之后,我创建了下面的查询
SELECT * FROM `table` WHERE title = 'Dr.' or title = 'Ms.' or title = 'Mr.' ) group by sentences
输出开始于
先生 女士 先生 博士
我想把博士。首先是数组中的女士而不是先生
只要您的输入经过净化,您就可以使用它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
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