Reputation: 241
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
Reputation:
you can do something like
order by if(name like 'ana', 1, if(name like 'adam', 2, 3))
Upvotes: 0
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