Reputation: 2524
How to order mysql results by column priority?
Example. I have a table of products, the table contains two columns, product name (p.name) and product description (p.desc).
Users should be able to enter keywords to find products in the database.
"p.name LIKE '%keyword%' OR p.desc LIKE '%keyword%'
I want the results that match p.name returned first and the p.desc second.
How would I go about achieving this?
Upvotes: 7
Views: 2632
Reputation: 68006
I would try something like
ORDER BY (NOT (p.name LIKE '%keyword%'))
If your first condition is satisfied, order by
clause will evaluate to false
. Thus, such records will be pushed ahead.
edit
Equals sign (=
) has probably got into the question by mistake.
Upvotes: 11
Reputation: 46692
You should try MySQL Full Text Search as in MATCH() ... AGAINST
and more here. Then order by the search rank with the columns.
Upvotes: 1