
- 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
Displaying only a list of records in ASC order with MySQL
To display a list of records in a specific order, you need to set conditions and use ORDER BY. For this, use ORDER BY CASE statement. Let us first create a table −
mysql> create table DemoTable2039 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2039 values('John Doe'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2039 values('John Smith'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable2039 values('Chris Brown'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable2039 values('Adam Smith'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2039 values('David Miller'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2039;
This will produce the following output −
+--------------+ | Name | +--------------+ | John Doe | | John Smith | | Chris Brown | | Adam Smith | | David Miller | +--------------+ 5 rows in set (0.00 sec)
Here is the query to display a specific list of records in ASC order −
mysql> select *from DemoTable2039 -> order by -> case when Name like '%Smith%' then 101 -> else -> 100 -> end, -> Name;
This will produce the following output −
+--------------+ | Name | +--------------+ | Chris Brown | | David Miller | | John Doe | | Adam Smith | | John Smith | +--------------+ 5 rows in set (0.37 sec)
- Related Articles
- Order By date ASC in MySQL?
- Order a column in MySQL with IP Address records?
- MySQL query to display ASC order in number column?
- Implement MySQL ORDER BY without using ASC or DESC?
- Order VARCHAR records with string and numbers in MySQL
- MySQL isn’t displaying right single quotation mark(’) after insertion of records
- MySQL ORDER BY ASC and display NULLs at the bottom?
- MySQL RegExp to fetch records with only a specific number of words
- Order by a single field and display rest of the records in the same order with MySQL
- Get records in a certain order using MySQL?
- MySQL regexp to display only records with strings or strings mixed with numbers. Ignore only the number records
- Selecting and displaying only some rows from a column in a MySQL table
- ORDER BY records in MySQL based on a condition
- Order MySQL records randomly and display name in Ascending order
- MySQL query to order records but fix a specific name and display rest of the values (only some) random

Advertisements