MySQL: Can you specify a random limit?

前端 未结 2 830
刺人心
刺人心 2021-01-27 10:52

Is there a way to randomize a limit number in SQL (MySQL)?

What I\'d like to be able to do is get a random number of results in a query to use in an insertion subquery w

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 11:44

    How about this:

        SELECT * 
          FROM users
         ORDER BY RAND()
         HAVING RAND() * 1000 < 10
    

    The clause WHERE RAND() * 1000 < 10 randomly chooses to include each row with a probability of 1%. It's not quite a LIMIT variable clause but will do roughly the same thing.

提交回复
热议问题