Reshuffle the values in a table with MySQL


To reshuffle the values in a table, use MySQL RAND().

Let us first create a table −

mysql> create table DemoTable792 (
   Name varchar(100),
   Subject varchar(100)
);
Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable792 values('Chris','MySQL');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable792 values('David','MySQL');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable792 values('Robert','MongoDB');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable792 values('Carol','MongoDB');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable792;

This will produce the following output -

+--------+---------+
| Name   | Subject |
+--------+---------+
| Chris  | MySQL   |
| David  | MySQL   |
| Robert | MongoDB |
| Carol  | MongoDB |
+--------+---------+
4 rows in set (0.00 sec)

Following is the query to reshuffle the values in a table −

mysql> select *from DemoTable792 order by rand();

This will produce the following output -

+--------+---------+
| Name   | Subject |
+--------+---------+
| Chris  | MySQL   |
| Robert | MongoDB |
| David  | MySQL   |
| Carol  | MongoDB |
+--------+---------+
4 rows in set (0.00 sec)

Updated on: 09-Sep-2019

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements