开发者

How to order mysql results by column priority?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜