
- 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 to get MySQL random integer range?
To get the random integer range, use the rand() function. The query to create a table −
mysql> create table RandomIntegerDemo −> ( −> Number int −> ); Query OK, 0 rows affected (0.61 sec)
Inserting records into table. The query is as follows −
mysql> insert into RandomIntegerDemo values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14); Query OK, 14 rows affected (0.14 sec) Records: 14 Duplicates: 0 Warnings: 0
Now you can display all records with the help of select statement. The query is as follows −
mysql> select *from RandomIntegerDemo;
The following is the output displaying integers −
+--------+ | Number | +--------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | | 11 | | 12 | | 13 | | 14 | +--------+ 14 rows in set (0.00 sec)
The query to generate the random integer range is as follows −
mysql> select Number, (FLOOR( 1 + RAND( ) *14 )) AS RandomValue −> from RandomIntegerDemo −> limit 0,14;
The output displays random integer range in the same table −
+--------+-------------+ | Number | RandomValue | +--------+-------------+ | 1 | 9 | | 2 | 8 | | 3 | 13 | | 4 | 13 | | 5 | 10 | | 6 | 10 | | 7 | 7 | | 8 | 3 | | 9 | 8 | | 10 | 2 | | 11 | 14 | | 12 | 6 | | 13 | 3 | | 14 | 9 | +--------+-------------+ 14 rows in set (0.00 sec)
- Related Articles
- Get Random value from a range of numbers in JavaScript?
- Get timestamp date range with MySQL Select?
- How to create a random number between a range JavaScript
- Integer Range in JavaScript
- How to create matrix with random integer values in R?
- Java Program to generate random number array within a range and get min and max value
- Get a random value between two values in MySQL?
- Python – Random range in a List
- How to generate random whole numbers in JavaScript in a specific range?
- Generate Random Integer Numbers in Java
- How to retrieve a random row or multiple random rows in MySQL?
- How to create a random vector for a range of values in R?
- How to update MySQL column with random value?
- How to treat MySQL longtext as integer in MySQL query?
- How to implement GROUP by range in MySQL?

Advertisements