Search code examples
mysqlsearchselect-query

How to get highest matching records first of select query mysql


Please do not down rate my question, because i'm new to MySQL. My Question is;

I have a table named 'item', and it has 'id', 'timestamp', 'title' columns. consider the table has following records in above order(id, timestamp, title).

100, 2016-01-12 12:26:30, sony bravia 32" Lcd TV

103, 2016-01-12 12:27:30, PSP 2 sony brand new

108, 2016-01-12 12:28:30, Xperia Aqua M2 brand new Sony mobile phone

Help me to create an SQL query to search 'title' column that shows results as highest matching records first.

As example, if we search "new Sony xperia phone" it should return results as following order.

108, 2016-01-12 12:28:30, Xperia Aqua M2 brand new Sony mobile phone

103, 2016-01-12 12:27:30, PSP 2 sony brand new

100, 2016-01-12 12:26:30, sony bravia 32" Lcd TV

Because highest matching record should come first.

108, 2016-01-12 12:28:30, Xperia Aqua M2 brand new Sony mobile phone

103, 2016-01-12 12:27:30, PSP 2 sony brand new

100, 2016-01-12 12:26:30, sony bravia 32" Lcd TV


Solution

  • I've found the answer. First make your appropriate columns 'Full Text' index. Then use the following query.

    SELECT * FROM table_name WHERE MATCH(`column_name`) AGAINST('your searching keywords');