MySql in array order?

I have a statement as shown below. The returned order is 1,4,5. My code expects 4,5,1 due to output priority rules. How to make mysql return the order I specified?

select * from Post where flag='0' and id in(4,5,1) 
+6
mysql
source share
5 answers
 select * from Post where flag='0' and id in(4,5,1) ORDER BY FIND_IN_SET(id, '4,5,1') 

MySQL Doc for FIND_IN_SET

+13
source share

Try a more explicit order: ORDER BY FIELD(id,4,5,1);

+4
source share

without a clause, order by mysql can return the result in whatever order it wants.

you need to specify the order with order by

0
source share

Try this instead:

 SELECT * FROM Post WHERE flag='0' AND id = 4 UNION SELECT * FROM Post WHERE flag='0' AND id = 5 UNION SELECT * FROM Post WHERE flag='0' AND id = 1 

This is terribly inefficient, but you will not need to modify your circuit.

0
source share

Possible Solution:

 select * from Post where flag='0' and id = 4 UNION select * from Post where flag='0' and id = 5 UNION select * from Post where flag='0' and id = 1 
0
source share

All Articles