How to order mysql results by column priority?

How to order mysql results by column priority?

Example. I have a product table, the table contains two columns, product name (p.name) and product description (p.desc).

Users should be able to enter keywords to search for products in the database.

"p.name LIKE '%keyword%' OR p.desc LIKE '%keyword%' 

I want results that match p.name to be returned first, and second to p.desc.

How can i achieve this?

+7
source share
2 answers

I would try something like

 ORDER BY (NOT (p.name LIKE '%keyword%')) 

If your first condition is met, the order by clause will be evaluated to false . Thus, such recordings will move forward.

change
The equal sign ( = ) probably got into the question by mistake.

+11
source

You should try MySQL Full Text Search, as in MATCH() ... AGAINST and more. Then sort by search rank with columns.

+1
source

All Articles