如何在SQL中设置随机值的下限

时间:2022-02-13 18:57:11

I want to insert multiple random values into one of my columns. However I don't want small values like 1.5 and so on. How do I set the lower bound for random numbers? For now I do it as follows:

我想在我的一个列中插入多个随机值。但是我不想要像1.5这样的小值。如何设置随机数的下限?现在我这样做:

delimiter $$  
create procedure randomizer()
begin
  declare i int Default 0 ;
  myloop: loop
  Insert into Tax  (id, amount) 
  VALUES (i+1, ROUND(RAND() * 1000,2));
  set i=i+1;
      if i=1000 then
      leave myloop;
end if;
end loop myloop;
end $$
delimiter ;

1 个解决方案

#1


2  

To set a lower bound, you just need to shift the values up by whatever min amount.

要设置下限,您只需将值向上移动任何最小量。

Insert into Tax  (id, amount) 
VALUES (i+1, ROUND(RAND() * 980,2) + 20 );

The above code will insert a random number between 20 and 1000

上面的代码将插入一个20到1000之间的随机数

#1


2  

To set a lower bound, you just need to shift the values up by whatever min amount.

要设置下限,您只需将值向上移动任何最小量。

Insert into Tax  (id, amount) 
VALUES (i+1, ROUND(RAND() * 980,2) + 20 );

The above code will insert a random number between 20 and 1000

上面的代码将插入一个20到1000之间的随机数