Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active May 20, 2020 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/cdcc5b4aaf60b8d5eaad9ba21588ffce to your computer and use it in GitHub Desktop.
Save Kcko/cdcc5b4aaf60b8d5eaad9ba21588ffce to your computer and use it in GitHub Desktop.
-- 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 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment