

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Order randomly in MySQL with a random value column?
- Order MySQL records randomly and display name in Ascending order
- How can I change the default sort order of MySQL tables?
- How can we use group functions with non-group fields in MySQL SELECT query?
- How can I make a table in MySQL called “order”?
- How can group functions be used in ORDER BY clause?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- How can I write in order with for loop or while loop?
- How to order results of a query randomly & select random rows in MySQL?
- Using MySQL, can I sort a column but allow 0 to come last?
- How can we create a MySQL view with GROUP BY clause?
- How to order or choose rows in MySQL GROUP BY clause?
- How can I create MySQL stored procedure with IN parameter?
- Can I use SUM() with IF() in MySQL?
- How to Order by date in MySQL but place empty dates in the end?
Advertisements