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?
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.
You should try MySQL Full Text Search as in MATCH() ... AGAINST
and more here. Then order by the search rank with the columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With