Select random number from a specific list

30
January 12, 2019, at 05:50 AM

How to get random number [10, 20, 30, 50] in MySQL.

I have tried using ROUND(RAND() * (50 - 10) + 10) but, this will generate numbers such as 11, 12, 24 which i don't want.

I wonder if this code will:

FLOOR((RAND([5,10,15,20,25,30,35,40]))

but how?

I want to set my quantity column to choose random 10,20,30, or 50. not 11, 12 or other possible number using ROUND(RAND() * (50-10) + 10).

Hope it is clear.

Answer 1

The following will give you one of the numbers from a list:

SELECT ELT(FLOOR(RAND() * 4) + 1, 10, 20, 30, 50);

Here 4 is the size of list and one is added since ELT is one-based.

Answer 2

try this it's working fine

ROUND(RAND() * (5-1))*10
Answer 3

If you want number that can be devised by 10 use Div and then multiply by 10

SELECT ((FLOOR(RAND()*(50-10+1))+10) DIV 10 ) * 10 ;

READ ALSO
Generate seperate XML file for each mysql table row using PHP

Generate seperate XML file for each mysql table row using PHP

I have this function in php script to generate xml file from mysql table

13
How do I sync a mysql table to a hive table ? (sqoop --incremental lastmodified hive imports is not supported)

How do I sync a mysql table to a hive table ? (sqoop --incremental lastmodified hive imports is not supported)

I want to sync a mysql table into hive tableBecause records in orders table usually changed in nearly future

51
How to execute PHP code with MySQL through command line?

How to execute PHP code with MySQL through command line?

I am trying to execute my PHP script through command line using below command:

22
I want to connect between MYSQL 5.7 with Crystal Rport using Windows Application framework 4

I want to connect between MYSQL 5.7 with Crystal Rport using Windows Application framework 4

Need a help for what are the drivers and version to install and connect MYSQL server 57 with Crystal Report for Windows Application Framework 4

10