0

MySQL/SQLite

I want to insert a randomly generated number (of 9 positions) into multiple rows BUT they need to be the same for all rows matched in the query.

update products set tag_seed=( SELECT ABS(RANDOM() % 999999999) ) where [...];

Partialy works... Each row will have a different random number. I need them to be same.

1 Answer 1

0

This is logical since you will generate a new random for every update query. The easiest solution is to generate a random integer, store it in a local variable and use that variable in your queries:

SET @rand := (SELECT ABS(RAND() * 1000000000));
update products set tag_seed=@rand where [...];
2
  • Thanks. Is it possible in SQLite?
    – user590584
    Commented Jun 9, 2013 at 0:34
  • SQLite does not support local variables as far as i know, you could store the random in a variable of your program though, or use a temp table with one field in which you temporarily store your value. Then you can fetch it in your update query using a subquery. Commented Jun 9, 2013 at 10:19

Your Answer

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