Reputation:
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?
select *
from Post
where flag='0' and id in(4,5,1)
Upvotes: 8
Views: 5056
Reputation: 176
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
Upvotes: 14
Reputation: 11790
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
Upvotes: 0
Reputation: 15699
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.
Upvotes: 0
Reputation: 265966
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
Upvotes: 0