
- 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 display records ordered by numeric difference?
Use ORDER BY and set the difference to display records ordered by numeric difference. Following is the syntax −
select *from yourTableName order by (yourIntegerColumnName1 - yourIntegerColumnName2);
Let us first create a table −
mysql> create table DemoTable1313 -> ( -> Name varchar(20), -> Score1 int, -> Score2 int -> ); Query OK, 0 rows affected (3.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1313 values('Chris',40,60); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1313 values('David',70,50); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1313 values('Adam',35,30); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1313;
Output
+-------+--------+--------+ | Name | Score1 | Score2 | +-------+--------+--------+ | Chris | 40 | 60 | | David | 70 | 50 | | Adam | 35 | 30 | +-------+--------+--------+ 3 rows in set (0.00 sec)
Following is the query to order by numeric difference −
mysql> select *from DemoTable1313 -> order by (Score1-Score2);
Output
+-------+--------+--------+ | Name | Score1 | Score2 | +-------+--------+--------+ | Chris | 40 | 60 | | Adam | 35 | 30 | | David | 70 | 50 | +-------+--------+--------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to display records ordered by DESC while skipping some?
- MySQL query to display only the records that contains single word?
- Display records by grouping dates in MySQL
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to subtract date records with week day and display the weekday with records
- MySQL query to display databases sorted by creation date?
- MySQL Query to set currency records
- Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- MySQL query to display records from a table filtered using LIKE with multiple words?
- MySQL query to group rows by the numeric characters in a string field?
- Fetch ordered records after a specific limit in MySQL
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- MySQL query to ORDER BY records on the basis of modulus result

Advertisements