2

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 different value to every row?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OR just run your query followed by UPDATE people SET column1 = 110 WHERE column1 BETWEEN 103 AND 109?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.