
- 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
ORDER BY a specific word in MySQL
For this, use ORDER BY INSTR(). Let us first create a table −
mysql> create table DemoTable822(Word text); Query OK, 0 rows affected (1.11 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable822 values('Forever'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable822 values('ever'); Query OK, 1 row affected (1.31 sec) mysql> insert into DemoTable822 values('every'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable822 values('everyday'); Query OK, 1 row affected (0.58 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable822;
This will produce the following output −
+----------+ | Word | +----------+ | Forever | | ever | | every | | everyday | +----------+ 4 rows in set (0.00 sec)
Following is the query to order by a specific word in the beginning or inside a string −
mysql> select *from DemoTable822 where Word LIKE '%ever%' order by INSTR(Word, 'ever'), Word;
This will produce the following output −
+----------+ | Word | +----------+ | ever | | every | | everyday | | Forever | +----------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL Order By specific strings?
- Order by on a specific number in MySQL?
- How to Order by a specific string in MySQL?
- ORDER BY specific field value first in MySQL
- MySQL Order by a specific column x and display remaining values in ascending order
- Sort a column ignoring a specific word in MySQL
- Remove specific word in a comma separated string with MySQL
- How to search specific word in MySQL with RegExp?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- How to arrange data in s specific order in MySQL?
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- Order a MySQL table by two columns?
- ORDER BY records in MySQL based on a condition
- Is there a default ORDER BY value in MySQL?

Advertisements