Petr Novák
Petr Novák

Reputation: 115

mysql procedure - random number from five numbers

I have 5 INT variables with numbers. I want select one random number from five variables in mysql procedure.

Example data:

DECLARE a, b, c, d, e INT;
SET a = 50;
SET B = 22;
SET C = 88;
SET D = 892;
SET E = 367;

Now I want to select one random number from variables a, b, c, d, e. Any ideas how to do it? Thanks.

Upvotes: 1

Views: 174

Answers (1)

Nitin Midha
Nitin Midha

Reputation: 2268

SELECT * FROM MyTable ORDER BY RAND() LIMIT 1

UPDATE

SELECT * FROM

(

SELECT @A AS Val

UNION ALL

SELECT @B AS Val

UNION ALL

SELECT @C AS Val

UNION ALL

SELECT @D AS Val

UNION ALL

SELECT @E AS Val

) F

ORDER BY RAND() LIMIT 1

Upvotes: 2

Related Questions