
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Get Random Value Between Two Values in MySQL
To get the random value between two values, use MySQL rand() method with floor(). The syntax is as follows.
select FLOOR( RAND() * (maximumValue-minimumValue) + minimumValue) as anyVariableName;
Let us check with some maximum and minimum value. The maximum value we are considering is 200 and minimum is 100. The random number will be between 100 and 200 including 100 and 200 itself.
The query is as follows.
mysql> select FLOOR( RAND() * (200-100) + 100) as RandomValue;
The following is the output.
+-------------+ | RandomValue | +-------------+ | 144 | +-------------+ 1 row in set (0.00 sec)
Now if we will run the same query again, the output will differ.
mysql> select FLOOR( RAND() * (200-100) + 100) as RandomValue;
The following is the output with a different value since these are random values between a range we set above.
+-------------+ | RandomValue | +-------------+ | 184 | +-------------+ 1 row in set (0.00 sec)
Advertisements