简体   繁体   English

用MySQL插入随机数

[英]Insert random number with mysql

i have a question in regards to mysql mainly. 我主要关于mysql有一个问题。 i have the following: 我有以下内容:

mysql_query("INSERT INTO {$game}_ships (location,login_id
         ) values(
    '250','44')");

i would like to make the location a random number from 2 to 300. 我想将位置设为2到300之间的随机数。

my table structure includes among others a column for location and login id, i dont need to mention other columns as they are static and do not change. 我的表结构除其他外包括位置和登录ID列,我不需要提及其他列,因为它们是静态的并且不会更改。 the above is suposed to make a ship look like its moving around the systems. 上面的假设使船看起来像它在系统中移动一样。

location   int(4)

can anyone help with this? 有人能帮忙吗?

mysql_query("INSERT INTO {$game}_ships (location,login_id
         ) values(
    FLOOR(RAND() * 298 + 2),'44')");

See: http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand 参见: http : //dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand

You can use something like this: 您可以使用如下形式:

INSERT INTO TABLE_NAME(rand_col) VALUES(FLOOR($MIN_VAL + (RAND() * ($MAX_VAL - $MIN_VAL))));

Where MIN_VAL is your lower number (2) and MAX_VAL is your higher number(300) 其中MIN_VAL是您的较低数字(2),而MAX_VAL是您的较高数字(300)

您可以改用PHP的rand吗?

mt_rand(2,300)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM