
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Sort search results based on substring position in MySQL
To sort search results based on substring position, use ORDER BY LOCATE(). Let us first create a table −
mysql> create table DemoTable1838 ( Subject varchar(100) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1838 values('MongoDB MySQL'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1838 values('MySQL Java'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1838 values('JavaWithMySQL'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1838;
This will produce the following output −
+---------------+ | Subject | +---------------+ | MongoDB MySQL | | MySQL Java | | JavaWithMySQL | +---------------+ 3 rows in set (0.00 sec)
Here is the query to sort search results based on substring position:
mysql> select * from DemoTable1838 where Subject LIKE '%MySQL%' ORDER BY LOCATE('MySQL', Subject);
This will produce the following output −
+---------------+ | Subject | +---------------+ | MySQL Java | | MongoDB MySQL | | JavaWithMySQL | +---------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to sort a vector in R based on manual position of elements?
- Sort HashMap based on keys in Java
- Google’s Relationships Based Voice Search on Android
- How can I search data from MySQL table based on similar sound values?
- Sort array based on another array in JavaScript
- Search and update array based on key JavaScript
- How to get substring results from a table with file location recordsi in MySQL?
- MySQL search results by month in format 2015-07-01 11:15:30?
- C program to sort triangles based on area
- How to use MySQL LIKE to create a search system and display results on the basis of keyword?
- Sort array based on presence of fields in objects JavaScript
- Sort array based on min and max date in JavaScript?
- Sort tuple based on occurrence of first element in Python
- Anagram Substring Search using Python
- Search Insert Position in C++

Advertisements