Set a MySQL column to random numbers, 1-line query

Useful query to seed a database table with random numbers. I used this for testing a support desk application:

UPDATE support_ticket SET priority = CAST((RAND() * 3)+1 AS UNSIGNED);

Where “3″ is the highest number in your range. By default, numbers range from 0-3, but in our case we needed to set the numbers from 1-4, so we set our max as 3 and add one at the end.

1 Comment

Filed under Database

One Response to Set a MySQL column to random numbers, 1-line query

  1. Thank you for this simple but extremely effective query. I used it, instead of randomly picking 100 numbers from 1-80. Worked perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>