
- 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
Can we order a MySQL result with mathematical operations?
Yes, we can order with mathematical operations using ORDER BY clause. Let us first create a table:
mysql> create table orderByMathCalculation -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Quantity int, -> Price int -> ); Query OK, 0 rows affected (0.57 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into orderByMathCalculation(Quantity,Price) values(10,50); Query OK, 1 row affected (0.21 sec) mysql> insert into orderByMathCalculation(Quantity,Price) values(20,40); Query OK, 1 row affected (0.14 sec) mysql> insert into orderByMathCalculation(Quantity,Price) values(2,20); Query OK, 1 row affected (0.13 sec) mysql> insert into orderByMathCalculation(Quantity,Price) values(11,10); Query OK, 1 row affected (0.24 sec)
Following is the query to display all records from the table using select statement:
mysql> select *from orderByMathCalculation;
This will produce the following output
+----+----------+-------+ | Id | Quantity | Price | +----+----------+-------+ | 1 | 10 | 50 | | 2 | 20 | 40 | | 3 | 2 | 20 | | 4 | 11 | 10 | +----+----------+-------+ 4 rows in set (0.00 sec)
Case 1: Here is the query to do order by mathematical operations in ascending order.
mysql> select *from orderByMathCalculation order by Quantity*Price;
This will produce the following output
+----+----------+-------+ | Id | Quantity | Price | +----+----------+-------+ | 3 | 2 | 20 | | 4 | 11 | 10 | | 1 | 10 | 50 | | 2 | 20 | 40 | +----+----------+-------+ 4 rows in set (0.00 sec)
Case 1: Here is the query to do order by mathematical operations in descending order.
mysql> select *from orderByMathCalculation order by Quantity*Price desc;
This will produce the following output
+----+----------+-------+ | Id | Quantity | Price | +----+----------+-------+ | 2 | 20 | 40 | | 1 | 10 | 50 | | 4 | 11 | 10 | | 3 | 2 | 20 | +----+----------+-------+ 4 rows in set (0.00 sec)
- Related Articles
- Perform mathematical operations in a MySQL Stored Procedure?
- Can we replace a number with a String in a MySQL result set?
- Can we use IFNULL along with MySQL ORDER BY?
- How can we create the MySQL view with ORDER BY clause?
- Performing mathematical operations in MySQL IF then ELSE is possible?
- How can we handle a result set inside MySQL stored procedure?
- How MySQL virtual GENERATED COLUMNS can work with mathematical expressions?
- How MySQL stored GENERATED COLUMNS can work with mathematical expressions?
- How can we write PHP script to release cursor memory associated with MySQL result?
- Can we use ORDER BY NULL in MySQL?
- Can we alter order of columns in MySQL?
- How can you order the result obtained by select query in MySQL?
- Can we use the result of a SUM() function in MySQL WHERE clause
- How can we sort MySQL output in descending order?
- How can we sort MySQL output in ascending order?

Advertisements