Update column with random value

会有一股神秘感。 提交于 2020-01-02 04:43:05

问题


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?


回答1:


Try

UPDATE TABLE SET VALUE=VALUE+ROUND(1+RAND()*4);



回答2:


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

UPDATE TABLEA SET VALUE=FLOOR(RAND()*5)+1


来源:https://stackoverflow.com/questions/12972470/update-column-with-random-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!