
- 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
How to ORDER BY RELEVANCE in MySQL?
To order by relevance, use the case statement. To understand the concept, let us create a table. The query to create a table is as follows −
mysql> create table OrderByRelevance -> ( -> UserId int, -> UserName varchar(200) -> ); Query OK, 0 rows affected (0.51 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into OrderByRelevance values(101,'Carol Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into OrderByRelevance values(102,'Carol Adams'); Query OK, 1 row affected (0.17 sec) mysql> insert into OrderByRelevance values(103,'Carolnathan Todd'); Query OK, 1 row affected (0.33 sec) mysql> insert into OrderByRelevance values(104,'John Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into OrderByRelevance values(105,'Sam Carol Bond'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from OrderByRelevance;
Output
+--------+------------------+ | UserId | UserName | +--------+------------------+ | 101 | Carol Smith | | 102 | Carol Adams | | 103 | Carolnathan Todd | | 104 | John Smith | | 105 | Sam Carol Bond | +--------+------------------+ 5 rows in set (0.00 sec)
Here is the query to order by relevance. The query is as follows −
mysql> select max(UserId)as Id,UserName from OrderByRelevance -> where UserName like '%Carol%' group by UserName -> order by case when UserName like 'Carol%' THEN 0 -> WHEN UserName like '% %Carol% %' THEN 1 -> WHEN UserName like '%Carol' THEN 2 -> else 3 -> end,UserName;
Output
+------+------------------+ | Id | UserName | +------+------------------+ | 102 | Carol Adams | | 101 | Carol Smith | | 103 | Carolnathan Todd | | 105 | Sam Carol Bond | +------+------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to ORDER BY LIKE in MySQL?
- How to order by auto_increment in MySQL?
- How to order by timestamp in MySQL?
- How to ORDER BY grouped fields in MySQL?
- How to sort by value with MySQL ORDER BY?
- How to order by date and time in MySQL?
- How to Order by a specific string in MySQL?
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- How to order MySQL rows by multiple columns?
- How to ORDER BY last 2 character string in MySQL?
- How to order last 5 records by ID in MySQL
- How to use union and order by clause in MySQL?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- How to order by certain part of a string in MySQL?
- How to order or choose rows in MySQL GROUP BY clause?

Advertisements