Ganesh Babu
Ganesh Babu

Reputation: 3670

How to return rows by MySQL in order of ids given inside IN function?

I have a table named resources. I am now using this query:

Select 
    Resource.id, Resource.status
from
    resources as Resource
where
    Resource.id IN ('53f4c1b8-7b60-42c9-ad88-324d16091b4e' , '53f4cb98-f5c4-448d-9bd4-3ac516091b4e',
        '53ccf77e-5ce0-4868-aa2e-395716091b4e',
        '53f4c9ba-693c-4c17-a16f-36d516091b4e')

When I queried, I got rows returned based on ordering id. ie.,The row having 2nd id is returned first. Well, it works like this. The rows are returned as per the ids in ascending order. Now, I need to return rows as per the input I have given. ie.,53f4c1b8-7b60-42c9-ad88-324d16091b4e should return as first row. What should I change in this query to get like this?

Upvotes: 0

Views: 58

Answers (1)

Mihai
Mihai

Reputation: 26784

where
    Resource.id IN ('53f4c1b8-7b60-42c9-ad88-324d16091b4e' , '53f4cb98-f5c4-448d-9bd4-3ac516091b4e',
        '53ccf77e-5ce0-4868-aa2e-395716091b4e',
        '53f4c9ba-693c-4c17-a16f-36d516091b4e')
ORDER BY FIELD( Resource.id,'53f4c1b8-7b60-42c9-ad88-324d16091b4e' , '53f4cb98-f5c4-448d-9bd4-3ac516091b4e',
        '53ccf77e-5ce0-4868-aa2e-395716091b4e',
        '53f4c9ba-693c-4c17-a16f-36d516091b4e')

Upvotes: 2

Related Questions