
- 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
MySQL query to sort multiple columns together in a single query
To sort multiple columns, use ORDER BY GREATEST(). Let us first create a −
mysql> create table DemoTable1395 -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1395 values(40,50,60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1395 values(90,56,80); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1395 values(10,20,30); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select −
mysql> select * from DemoTable1395;
This will produce the following output −
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 40 | 50 | 60 | | 90 | 56 | 80 | | 10 | 20 | 30 | +--------+--------+--------+ 3 rows in set (0.00 sec)
Here is the query to sort multiple columns−
mysql> select * from DemoTable1395 -> order by greatest(Value1,Value2,Value3);
This will produce the following output −
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 10 | 20 | 30 | | 40 | 50 | 60 | | 90 | 56 | 80 | +--------+--------+--------+ 3 rows in set (0.00 sec)
- Related Articles
- Change multiple columns in a single MySQL query?
- How to sort multiple columns with a single query?\n
- How can we sort multiple columns in a single query?
- How to alter column type of multiple columns in a single MySQL query?
- Implement multiple COUNT() in a single MySQL query
- MySQL update multiple records in a single query?
- Insert multiple rows in a single MySQL query
- MySQL query to GROUP BY multiple columns
- Multiple COUNT() for multiple conditions in a single MySQL query?
- MySQL query to combine two columns in a single column?
- Update two columns with a single MySQL query
- MySQL query to increase item value price for multiple items in a single query?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- Implement multiple LIKE operators in a single MySQL query

Advertisements