How to get mysql random integer range?

后端 未结 4 2109
执笔经年
执笔经年 2020-12-08 06:01

I am trying to generate a random integer for each row I select between 1 and 60 as timer.

SELECT downloads.date, products.*, (FLOOR(1 + RAND() * 60)) AS time         


        
相关标签:
4条回答
  • 2020-12-08 06:45

    This is working for me. Your mysql version maybe?

    SELECT id, (FLOOR( 1 + RAND( ) *60 )) AS timer
    FROM users
    LIMIT 0 , 30
    
    0 讨论(0)
  • 2020-12-08 06:46

    I'm running your query and it does give me a random number for each row.... maybe has something to do with the name of the random (timer)?

    0 讨论(0)
  • 2020-12-08 06:52

    The output of the RAND function will always be a value between 0 and 1.

    Try this:

    SELECT downloads.date, products.*, (CAST(RAND() * 60 AS UNSIGNED) + 1) AS timer
    
    0 讨论(0)
  • 2020-12-08 06:58

    You can increase the number multiplied by the number of records in the table.

    SELECT id,
    
    (FLOOR( (SELECT MIN(id)                        FROM  your_table ) + RAND( ) * 1000000 ) ) AS timer
    FROM your_table
    LIMIT 0 , 30
    
    0 讨论(0)
提交回复
热议问题