使用 MYSQL 创建一个随机数

2021-11-20 00:00:00 sql mysql

我想知道有没有办法选择 100 到 500 之间随机生成的数字以及选择查询.

I would like to know is there a way to select randomly generated number between 100 and 500 along with a select query.

例如:SELECT name, address, random_number FROM users

我不必将此数字存储在 db 中,仅用于显示目的.

I dont have to store this number in db and only to use it to display purpose.

我尝试过类似这样的方法,但无法正常工作..

I tried it something like this, but it can't get to work..

SELECT name, address, FLOOR(RAND() * 500) AS random_number FROM users

希望有人帮助我.谢谢

推荐答案

这应该给你想要的:

FLOOR(RAND() * 401) + 100

通常,FLOOR(RAND() * ( - + 1)) + 生成之间的数字>> 和 包括在内.

Generically, FLOOR(RAND() * (<max> - <min> + 1)) + <min> generates a number between <min> and <max> inclusive.

更新

这个完整的声明应该有效:

This full statement should work:

SELECT name, address, FLOOR(RAND() * 401) + 100 AS `random_number` 
FROM users

相关文章