Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update records with random number

I have a 200k rows mysql table, and a column with empty value, and i like to populate this column with random numbers from 1 to 10,000.

I don't know if its possible to have a query something like this:

UPDATE 'videos'
SET 'views' = rand(1,10000)
like image 771
user3047473 Avatar asked Nov 29 '13 14:11

user3047473


People also ask

How do you UPDATE a column in SQL with random numbers?

UPDATE yourTableName set yourColumnName=value where yourColumnName2=(SELECT FLOOR(1+RAND()*3)); In the above query, the statement FLOOR(1+RAND()*3) generates the number between 1-3 and update the column.

How do I select a random number in SQL query?

SQL Server RAND() Function The RAND() function returns a random number between 0 (inclusive) and 1 (exclusive).

How do I UPDATE multiple rows at once?

There are a couple of ways to do it. INSERT INTO students (id, score1, score2) VALUES (1, 5, 8), (2, 10, 8), (3, 8, 3), (4, 10, 7) ON DUPLICATE KEY UPDATE score1 = VALUES(score1), score2 = VALUES(score2);


2 Answers

RAND() produces random float values from 0 to 1. Could you try this?

Update videos set views = CAST(RAND() * 10000 AS UNSIGNED)
like image 189
Jason Heo Avatar answered Oct 23 '22 18:10

Jason Heo


Try this one. It can generate a random number between 1 to 10000.

UPDATE videos
SET views = FLOOR(rand() * 10000) + 1;
like image 40
Faisal Avatar answered Oct 23 '22 18:10

Faisal