
- 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 MySQL rows by multiple columns?
Let us first create a table −
mysql> create table DemoTable -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (1.44 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Smith | | Chris | Brown | | Carol | Taylor | | David | Miller | +-----------+----------+ 4 rows in set (0.00 sec)
Following is the query to order MySQL rows by multiple columns −
mysql> select *from DemoTable order by FirstName,LastName
Output
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Carol | Taylor | | Chris | Brown | | David | Miller | | John | Smith | +-----------+----------+ 4 rows in set (0.24 sec)
- Related Articles
- Order by multiple columns not working as expected in MySQL?
- MySQL filtering by multiple columns?
- MySQL query to GROUP BY multiple columns
- How to convert multiple columns to rows in Excel?
- How to Convert Multiple Rows to Columns in Excel?
- Order MySQL query by multiple ids?
- How to AutoSum Multiple Rows and Columns in Excel?
- How to AutoSum Multiple Rows/Columns/Worksheets in Excel?
- How to order or choose rows in MySQL GROUP BY clause?
- Order a MySQL table by two columns?
- How to search multiple columns in MySQL?
- Concatenate multiple rows and columns in a single row with MySQL
- How to order by the highest value from two columns in MySQL?
- Multiple LIKE Operators with ORDER BY in MySQL?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?

Advertisements