Update column with random value

2019-05-04 17:52发布

I have a table:

ID | VALUE | DATE
1  | 5     | 2012-10-01 
2  | 7     | 2012-10-02
3  | 3     | 2012-10-05
4  | 0     | 2012-05-07 

I want to add on the top of the current value with the VALUE random individually BETWEEN 1 AND 5.

Let say:

ID | VALUE | RANDOM VALUE
1  | 5     | 0
2  | 7     | 2
3  | 3     | 3
4  | 0     | 6 

NEW VALUE

ID | VALUE 
1  | 5     
2  | 9     
3  | 6     
4  | 6      

How do I do this? All I can think of is by doing cursor type of query.

Any help?

2条回答
爷的心禁止访问
2楼-- · 2019-05-04 18:24

Try

UPDATE TABLE SET VALUE=VALUE+ROUND(1+RAND()*4);
查看更多
时光不老,我们不散
3楼-- · 2019-05-04 18:27

This will update the value by a random value between 1 and 5

UPDATE TABLEA SET VALUE=FLOOR(RAND()*5)+1
查看更多
登录 后发表回答