0

I have and arrray below

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

after foreach loop, i create the query below

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

OUTPUT start from

Mr. Ms. Mr. Dr.

I want to PUT the DR. first that Ms. than Mr. which is in order inside of the array()

2 Answers 2

4

As long as your input is sanitized, you can use implode to prepare the statement.

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

Result

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

See a demo

You can alternatively use IN:

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

Result

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

You need to fix your GROUP BY; you're not using it correctly.

If you want to sort the order of your titles, you can try:

ORDER BY
  CASE
    WHEN title = 'Dr' THEN 1
    WHEN title = 'Ms.' THEN 2
    WHEN title = 'Mr.' THEN 3
    ELSE 4
  END
Sign up to request clarification or add additional context in comments.

1 Comment

still not in order SELECT * FROM table WHERE title IN ( 'Dr.', 'Ms.', 'Mr.' ) LIMIT 0 , 30
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.