
- 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 ORDER BY records on the basis of modulus result
For this, use ORDER BY with a modulus operator. Let us first create a table −
mysql> create table DemoTable ( StudentId int, StudentName varchar(100) ); Query OK, 0 rows affected (1.88 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(101,'Robert'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(102,'David'); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(103,'Mike'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 100 | Chris | | 101 | Robert | | 102 | David | | 103 | Mike | +-----------+-------------+ 4 rows in set (0.00 sec)
Here is the query to order by with modulus −
mysql> select StudentId,StudentName,StudentId % 2,StudentId % 3 from DemoTable order by StudentId % 2,StudentId % 3;
This will produce the following output −
+-----------+-------------+---------------+---------------+ | StudentId | StudentName | StudentId % 2 | StudentId % 3 | +-----------+-------------+---------------+---------------+ | 102 | David | 0 | 0 | | 100 | Chris | 0 | 1 | | 103 | Mike | 1 | 1 | | 101 | Robert | 1 | 2 | +-----------+-------------+---------------+---------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- MongoDB query to count records on the basis of matching criteria
- MySQL query to select records from a table on the basis of a particular month number?
- Add a column count in a MySQL query on the basis of last name records?
- Search records on the basis of date in MySQL?
- How can you order the result obtained by select query in MySQL?
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- Select items based on value first, then order on the basis of date for rest of the records in MySQL
- Fetch records on the basis of LastName using MySQL IN()
- ORDER BY records in MySQL based on a condition
- How to get MySQL query result in same order as given by IN clause?
- Display records on the basis of key-value pairs in MySQL
- MySQL query to get result by month and year based on condition?
- Select records from a table on the basis of keywords in MySQL
- Display the count of duplicate records from a column in MySQL and order the result

Advertisements