
- 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 can I order in group but randomly with MySQL?
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value char(1) ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value) values('X'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Value) values('Y'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Value) values('X'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable(Value) values('X'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Value) values('Y'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Value) values('Z'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Value) values('Z'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 1 | X | | 2 | Y | | 3 | X | | 4 | X | | 5 | Y | | 6 | Z | | 7 | Z | +----+-------+ 7 rows in set (0.00 sec)
Case 1 − Following is the query to group but randomly −
mysql> select DemoTable.* from DemoTable join ( select Value, rand() as `random_Value` from DemoTable group by Value ) tbl2 on DemoTable.Value = tbl2.Value order by tbl2.`random_Value`;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 6 | Z | | 7 | Z | | 1 | X | | 3 | X | | 4 | X | | 2 | Y | | 5 | Y | +----+-------+ 7 rows in set (0.00 sec)
Case 2 − Let us run the above query again to display since we are showing random records −
mysql> select DemoTable.* from DemoTable join ( select Value, rand() as `random_Value` from DemoTable group by Value ) tbl2 on DemoTable.Value = tbl2.Value order by tbl2.`random_Value`;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 2 | Y | | 5 | Y | | 6 | Z | | 7 | Z | | 1 | X | | 3 | X | | 4 | X | +----+-------+ 7 rows in set (0.00 sec)
- Related Articles
- Order randomly in MySQL with a random value column?
- Order MySQL records randomly and display name in Ascending order
- How can I make a table in MySQL called “order”?
- How to order results of a query randomly & select random rows in MySQL?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- How can we use group functions with non-group fields in MySQL SELECT query?
- How can I change the default sort order of MySQL tables?
- How can group functions be used in ORDER BY clause?
- How can I write in order with for loop or while loop?
- How can I avoid “repair with keycache” in MySQL?
- How to order or choose rows in MySQL GROUP BY clause?
- How can we create a MySQL view with GROUP BY clause?
- How can we create the MySQL view with ORDER BY clause?
- How can I create MySQL stored procedure with IN parameter?
- Can we use IFNULL along with MySQL ORDER BY?

Advertisements