MySql in array order?
I have a statement like the below. The order returned is 1,4,5. My code expects 4,5,1 because of output precedence rules. How do i make mysql return the order i specified?
se开发者_Go百科lect *
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 ordering:
ORDER BY FIELD(id,4,5,1);
without an order by clause mysql is free to return the result in any order it wants.
you'd have to specify the ordering with 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
It's horribly inefficient, but it won't require you to change your schema.
A 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
精彩评论