user1610904
user1610904

Reputation: 397

I need an SQL query that populates column with random numbers within min/max range

Okay so I have 2 columns in a table called "user". The columns are called "avatarid" and "avatarrevision"

For my query, I need to populate all row entries for "avatarid" with random numbers between 1 and 7 only where "avatarrevision" currently equals "0".

I have no idea how to write the random number stuff. Any ideas?

Upvotes: 1

Views: 94

Answers (2)

SagarPPanchal
SagarPPanchal

Reputation: 10111

UPDATE user SET avatarid = avatarid + FLOOR(RAND() * (1)) + 7 where avatarrevision = 0;

Upvotes: -1

Gordon Linoff
Gordon Linoff

Reputation: 1269583

You can do this with update and rand():

update user
    set avatarid = floor(1 + rand() * 7)
    where avatarrevision = 0;

Upvotes: 4

Related Questions