MYSQL query issue with UPDATE
I have this query:
UPDATE people
SET column1 = (
SELECT if(r.rand BETWEEN 103 AND 109, 110, r.rand)
FROM ( SELECT floor(8+rand()*113) rand ) r
)
However, it sets the SAME random number to every row. How can I change it in order to give a dif开发者_运维技巧ferent value to every row?
It seems the problem comes from the subquery, if you could simplify your assignment like the following:
UPDATE people
SET column1 = floor(8+rand()*113)
It would work the way you expect: each row has assigned a different random number. However the subquery returns a single value that is assigned to all the rows.
I guess that if you can't simplify the logic to remove the scalar subquery, you may have to write a stored procedure or a short program to do what you want.
精彩评论