James Bell
James Bell

Reputation: 614

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 want 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

But I want it to return:

I am new to mysql full search text.

Upvotes: 7

Views: 2312

Answers (1)

a'r
a'r

Reputation: 36999

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;

Upvotes: 2

Related Questions