Original post: http://anothermysqldba.blogspot.com/2014/06/mysql-random-integers.html
這是不以任何手段的一項新功能,但它是一個問題,我也碰巧看到彈出飄飛。 所以,一個簡單的例子是下面的。
到MySQL中生成一個隨機整數您可以使用地板和蘭德功能。 MySQL手冊此文件在這裡:http://dev.mysql.com/doc/refman/5.5/en/mathematical-functions.html#function_rand
“ 為了獲得範圍內的隨機整數R I <= R <J2,使用表達式FLOOR(i + RAND()*(十-一)) “
所以,舉個例子:
到MySQL中生成一個隨機整數您可以使用地板和蘭德功能。 MySQL手冊此文件在這裡:http://dev.mysql.com/doc/refman/5.5/en/mathematical-functions.html#function_rand
“ 為了獲得範圍內的隨機整數R I <= R <J2,使用表達式FLOOR(i + RAND()*(十-一)) “
所以,舉個例子:
> SET @I = 3; # lower range
> SET @J = 43 - @I; # max range minus lower range
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G
*************************** 1. row ***************************
lottery_numbers: 4
1 row in set (0.00 sec)
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G
*************************** 1. row ***************************
lottery_numbers: 8
1 row in set (0.00 sec
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G
*************************** 1. row ***************************
lottery_numbers: 15
1 row in set (0.00 sec
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G
*************************** 1. row ***************************
lottery_numbers: 16
1 row in set (0.00 sec
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G
*************************** 1. row ***************************
lottery_numbers: 23
1 row in set (0.00 sec
> SELECT FLOOR( @I + (RAND() * @J )) as lottery_numbers\G
*************************** 1. row ***************************
lottery_numbers: 42
1 row in set (0.00 sec