Skip to content
Advertisement

How to insert a random integer number, for a given column and several rows

I was wondering.. Is it possible, with a SQL query, to:

  • generate randomly, a list of integer number
  • insert this in a table, for a specific column, and for multiple rows

I didn’t try anything, to be honest I was just asking myself the question.

EDIT: (from a comment)

Imagine that I have a table USERS, with a field named VOTES. For every users, I want to insert, in the column VOTES, a random integer number between 0 and 15. For the record, I’m working with MySQL 5.5 | MariaDB

Thanks for your answers

Advertisement

Answer

If you want to update the column named votes in the table users with random integers between 0 and 15, you could do it in the following:

UPDATE users
    SET votes = FLOOR(RAND() * 16)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement