Insert Random Number in Column MySQLi Query

I have one table called videos in MySQL database. It have column called likes and it have about 500 records in it. column likes currently have 0 values in all rows. I want set random value for that column like 10,50,11,23 etc between 10 to 100. I am new in PHP and does not know how I can do it. I have tried like below query

UPDATE videos SET likes = 10;

but its setting 10 for all rows, instead I want random number there.

Thanks

3 answers

  • answered 2019-10-22 08:24 FrankerZ

    Use RAND() function:

    UPDATE videos SET likes = FLOOR(RAND()*(100-10)+10);
    

    RAND() will generate a number from 0 to 1. Simply multiply it by the range of your options, and add the initial low value (In this case 10). Use FLOOR() on the final result, to round it down to the nearest integer:

  • answered 2019-10-22 08:26 Tim Biegeleisen

    Use RAND():

    UPDATE videos
    SET likes = FLOOR(RAND()*(100-10+1)+10);
    

    Note that using FLOOR here is necessary, because the RAND() function generates a uniformly distributed floating point value between 0 and 1.

  • answered 2019-10-22 08:30 Rp9

    use Rand to generate number

       UPDATE videos SET likes = FLOOR(10 + RAND() * (100-10));