/ Gists / Update table col by random date / string
On gists

Update table col by random date / string

MySql MySql tricks MySql - advanced

update.sql Raw #

-- datetime
SET @MIN = '2019-06-29 00:53:27';
SET @MAX = '2019-06-29 13:53:27';

UPDATE tablename
SET columnname = TIMESTAMPADD(SECOND, FLOOR(RAND() * TIMESTAMPDIFF(SECOND, @MIN, @MAX)), @MIN)

-- https://stackoverflow.com/questions/24378490/update-table-with-random-values-from-given-set-of-string-values
-- string
update table t
    set col = elt(floor(rand()*3) + 1, 'value1', 'value2', 'value3');
    
-- or
UPDATE `table`
SET `column`=(CASE CEIL(RAND()*3)
              WHEN 1 THEN 'value1'
              WHEN 2 THEN 'value2'
              WHEN 3 THEN 'value3'
          END);
          
          
-- random numbet between N a M

-- For range (min..max( (min inclusive, max exclusive) it is:
FLOOR( RAND() * (max-min) + min )
 
-- For range (min..max) (min+max inclusive) it is:
FLOOR( RAND() * (max-min+1) + min )