
- 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 change order of items in MySQL?
To change order of items in MySQL, use ORDER BY alias name. Let us first create a table −
mysql> create table DemoTable653 (Product1Amount int,Product2Amount int); Query OK, 0 rows affected (0.42 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable653 values(400,250); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable653 values(500,300); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable653 values(40,400); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable653 values(200,450); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable653 values(50,20); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable653;
This will produce the following output −
+----------------+----------------+ | Product1Amount | Product2Amount | +----------------+----------------+ | 400 | 250 | | 500 | 300 | | 40 | 400 | | 200 | 450 | | 50 | 20 | +----------------+----------------+ 5 rows in set (0.00 sec)
Following is the query to change the order of items and display the difference −
mysql> select Product1Amount,Product2Amount,(Product1Amount-Product2Amount) as ProductAmountDifference from DemoTable653 order by ProductAmountDifference;
This will produce the following output −
+----------------+----------------+-------------------------+ | Product1Amount | Product2Amount | ProductAmountDifference | +----------------+----------------+-------------------------+ | 40 | 400 | -360 | | 200 | 450 | -250 | | 50 | 20 | 30 | | 400 | 250 | 150 | | 500 | 300 | 200 | +----------------+----------------+-------------------------+ 5 rows in set (0.29 sec)
- Related Articles
- How can I change the default sort order of MySQL tables?
- How to change background color of TableView items on iOS?
- How to change the JOIN order in Oracle?
- How to change the order of Pandas DataFrame columns?
- How to change the background color of ListView items on Android?
- How to change the order of plots in Pandas hist command?
- How to change the order of bars in bar chart in R?
- How to change the order of elements in a list in R?
- How to change the order of a matrix in increasing order based on a single column?
- How to change the background color of ListView items on Android Kotlin?
- How to change the order of columns in an R data frame?
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?
- How to order by timestamp in MySQL?
- How to order by auto_increment in MySQL?

Advertisements