
- 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
Shuffling column values with MySQL?
To shuffle elements, you need to use ORDER BY RAND(). Let us first create a table −
mysql> create table DemoTable1557 -> ( -> SubjectId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> SubjectName varchar(20) -> ); Query OK, 0 rows affected (0.91 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1557(SubjectName) values('MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1557(SubjectName) values('MongoDB'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1557(SubjectName) values('Java'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1557(SubjectName) values('C'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select −
mysql> select * from DemoTable1557;
This will produce the following output −
+-----------+-------------+ | SubjectId | SubjectName | +-----------+-------------+ | 1 | MySQL | | 2 | MongoDB | | 3 | Java | | 4 | C | +-----------+-------------+ 4 rows in set (0.00 sec)
Following is the query to shuffle records with MySQL −
mysql> select * from DemoTable1557 order by rand();
This will produce the following output −
+-----------+-------------+ | SubjectId | SubjectName | +-----------+-------------+ | 1 | MySQL | | 3 | Java | | 2 | MongoDB | | 4 | C | +-----------+-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Using MySQL IN() for some column values with underscore
- MySQL query to display only the column values with corresponding column having whitespace
- Inserting multiple parameter values into a single column with MySQL?
- Pad the values of the column with zeros in MySQL
- MySQL query to display record with maximum count values in a group with other column values?
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- MySQL randomly select 2 values from column values?
- MySQL query to select column values ending with certain character/number?
- Extract tuples with specified common values in another column in MySQL?
- MySQL query to retrieve only the column values with special characters?
- Add records from corresponding duplicate values in another column with MySQL
- MySQL UPDATE column names and set None values with N/A?
- Get all the records with two different values in another column with MySQL
- Swapping two column values in MySQL?

Advertisements