
- 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
Select random number from a specific list in MySQL?
You can use elt() along with rand() for this. Let us select random number from a specific list.
mysql> SELECT ELT(FLOOR(RAND() * 10) + 1, 100,200,300,400,500,600,700,800,900,1000) AS random_value_from_listOfValues;
This will produce the following output −
+--------------------------------+ | random_value_from_listOfValues | +--------------------------------+ | 1000 | +--------------------------------+ 1 row in set (0.00 sec)
Now we will run the query again to select random number from a specific list.
mysql> SELECT ELT(FLOOR(RAND() * 10) + 1, 100,200,300,400,500,600,700,800,900,1000) AS random_value_from_listOfValues;
This will produce the following output. This would be different from the above output since we are displaying random numbers −
+--------------------------------+ | random_value_from_listOfValues | +--------------------------------+ | 400 | +--------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Select a fixed number of random records from a MySQL table?
- What is the most efficient way to select a specific number of random rows in MySQL?
- How to select a random element from a C# list?
- Select a random row in MySQL
- How to select a random record from a MySQL database?
- MySQL query to select one specific row and another random row?\n
- Select all records if it contains specific number in MySQL?
- MySQL query to select records beginning from a specific id
- Select two random rows in a MySQL database?
- MySQL - Select all records if it contains specific number?
- How to get a specific column record from SELECT query in MySQL?
- Distinct number of specific items in list with MySQL
- Select specific rows in a range with MySQL?
- How select specific rows in MySQL?
- Select random row that exists in a MySQL table?

Advertisements