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)
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
Try a more explicit order: ORDER BY FIELD(id,4,5,1);
ORDER BY FIELD(id,4,5,1);
without a clause, order by mysql can return the result in whatever order it wants.
you need to specify the order with order by
order by
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.
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