开发者

Ordering sql query by first letter

I am trying to order my mysql queries based on the first letter but every method I have used is restricting my search.

Example MySQL table Value computer services abc computer services dynamic computer services

If I search for computer services I w开发者_JS百科ant the results to returned as:

**Name**
computer services
abc computer services
dynamic computer services

I am using mysql fullsearch text but if i use name LIKE 'c%' I don't the other two results e.g.

SELECT name FROM table WHERE match('name') against('computer services*' IN BOOLEAN MODE) AND name LIKE 'c%';

This would only return

  • computer services

But I want it to return:

  • computer services
  • abc computer services
  • dynamic computer services

I am new to mysql full search text.


Use an order by clause that matches the 'starts with' case first. I'm using not like here because the boolean returns 0 or 1 and we want to reverse that to match the starts with case first.

SELECT name 
FROM table 
WHERE match('name') against('computer services*' IN BOOLEAN MODE)
ORDER BY name NOT LIKE 'computer services%', name;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜