Szmulik
Szmulik

Reputation: 241

Mysql order by OR sequence

I have MYSQL query:

SELECT * FROM  user WHERE name LIKE '%ana%' 
OR name LIKE '%adam%' 
OR name LIKE '%pit%'

Is any way to order result by OR sequence

I mean to display results with name LIKE '%ana%' as first name LIKE '%adam%' as second name LIKE '%pit%' as third.

Upvotes: 0

Views: 124

Answers (2)

user4843530
user4843530

Reputation:

you can do something like

order by if(name like 'ana', 1, if(name like 'adam', 2, 3))

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270401

First, use IN for the where clause (you don't have wildcards, so this is actually more efficient):

where name in ('ana', 'adam', 'pit')

Second, use find_in_set() for the ordering:

order by find_in_set(name, 'ana', 'adam', 'pit')

Upvotes: 2

Related Questions