This is similar to other questions but it appears to vary on a database-by-database implementation. I am using postgres and things like NEWID
do not appear to exist.
This is what I want to work:
update foo set bar = (select floor(random() * 10) + 1);
I understand why it doesn't but I can't seem to find a suitable solution that works for Postgres. I want the value of bar
to be a random number between 1 and 10 that differs per row.
I think that Postgres might optimize the subquery. Doesn't this work?
update foo
set bar = floor(random() * 9 + 1); -- from 1 to 10, inclusive
update foo set bar = floor(random() * 10) + 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With