mysql的隨機查詢

SELECT * FROM tablename ORDER BY RAND() LIMIT 10

讓我們去Mysql的官網去看看,這個內置函數。 RAND([N])

Returns a random floating-point value v in the range 0 <= v < 1.0. To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j − i)). For example, to obtain a random integer in the range the range 7 <= R < 12, use the following statement:

SELECT FLOOR(7 + (RAND() * 5)); If an integer argument N is specified, it is used as the seed value:

With a constant initializer argument, the seed is initialized once when the statement is prepared, prior to execution.

With a nonconstant initializer argument (such as a column name), the seed is initialized with the value for each invocation of RAND().

One implication of this behavior is that for equal argument values, RAND(N) returns the same value each time, and thus produces a repeatable sequence of column values. In the following example, the sequence of values produced by RAND(3) is the same both places it occurs. 根據介紹,可以看出這就是個獲取0-1之間隨機數的函數。 不過這種應該可以滿足隨機取出數據的要求。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章