开发者

How to order mysql results by column priority?

开发者 https://www.devze.com 2023-02-07 11:50 出处:网络
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 d开发者_Python百科escription (p.desc).

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 d开发者_Python百科escription (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号