常用的代码
update `表名` set `字段名`=ceiling(rand()*500000+500000) where (条件);
update `表名` set click=click*0.01 where classid='2' and click>2000
我们经常会遇到使用随机的问题,下面就是一种解决随机数的方法。
在构造测试数据时,我们需要对测试表插入随机数据。构造测试数据的方法如下,仅以update为例说明
步骤1:随机数的sql函数为rand() ,而rand()生成的是0-1之间的小数。
步骤2:将rand()*10
将产生1-10之间的带小数的数字,可以使用ceil进行转换。
步骤3:使用cast做类型转换
cast的用法如下:
cast(value as type)
二进制,同带binary前缀的效果 : binary
字符型,可带参数 : char()
日期 : date
时间: time
日期时间型 : datetime
浮点数 : decimal
整数 : signed
无符号整数 : unsigned
select cast(rand()*10 as signed) as rand
步骤4:运行sql语句
update storm_effect_info s set
s.fhtp = (select cast(rand()*10 as signed) as rand)where s.id = 13