
- 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
Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?
Yes, you can perform this in MySQL by first getting the middle value. Let us first create a table:
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK, 0 rows affected (0.65 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.10 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output
+--------+ | UserId | +--------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | +--------+ 10 rows in set (0.00 sec)
Here is the query to get the middle value first:
mysql> set @middleValue=(select max(UserId) from DemoTable)/2; Query OK, 0 rows affected (0.01 sec)
Now, let us get the ascending and descending order value alternatively:
mysql> select *from DemoTable ORDER BY (IF(UserId <@middleValue,@middleValue*2- UserId,UserId-1)) DESC,UserId ASC;
This will produce the following output
+--------+ | UserId | +--------+ | 1 | | 10| | 2 | | 9 | | 3 | | 8 | | 4 | | 7 | | 6 | | 5 | +--------+ 10 rows in set (0.00 sec)
- Related Articles
- Order MySQL records randomly and display name in Ascending order
- Is it possible to sort varchar data in ascending order that have both string and number values with MySQL?
- How to arrange the fractions in ascending order and descending order?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- MySQL order by 0 first and then display the record in descending order?
- MySQL Order by a specific column x and display remaining values in ascending order
- Select last 20 records ordered in ascending order in MySQL?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- Program to merge intervals and sort them in ascending order in Python
- How to show records in descending order Android sqlite?
- Recursion example in JavaScript to display numbers is descending order?
- Program to find overlapping intervals and return them in ascending order in Python
- MySQL ORDER BY with different ordering for some of the values as descending and others ascending?
- Find and display duplicate records in MySQL?
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python

Advertisements