Refresh a table with random values ​​from a given set of string values

I want to update my table with random values ​​from a given set, and not from another table.

eg. value1, value2, value3

and the MySQL query should update all records from the above values.

I'm looking for a similar type of solution, but with random string values ​​from a given set: Refresh a column with a random value

+4
source share
3 answers

Use floor(rand()*3)to generate a random number among 0, 1 and 2, then use case whento assign a value

update test
set i = (case floor(rand()*3) 
         when 0 then 0 
         when 1 then 10 
         when 2 then 20 
         end);

fiddle

+7
source

I would use the function elt():

update table t
    set col = elt(floor(rand()*3) + 1, 'value1', 'value2', 'value3');

elt() . rand() .

+12

Try CEIL and RAND function

UPDATE `table`
SET `column`=(CASE CEIL(RAND()*3)
              WHEN 1 THEN 'value1'
              WHEN 2 THEN 'value2'
              WHEN 3 THEN 'value3'
          END);
+4
source

Source: https://habr.com/ru/post/1545785/


All Articles