Latest Post

Error: No input file specified.

While trying to run a codeigniter app the other day I came across this error just after I had finished uploading the files and making the ne...

Wednesday, January 29, 2014

MYSQL - Generate a random number

Syntax:

SELECT FLOOR(RAND() * ( MAX - MIN  + 1)) + MIN 

This will generate a number between  MIN and MAX inclusive.

Example usage:

Say we want to generate a random number between 1 and 10 we would say:

SELECT FLOOR(RAND() * (10 - 1 + 1)) + 1 

i.e.

SELECT FLOOR(RAND() * 10) + 1 

Hope this helps someone.


No comments:

Post a Comment

Chitika Ads