萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql查詢隨機幾條數據

mysql查詢隨機幾條數據

  MySql查詢隨機幾條數據

  想到了 Max RAND 這幾個函數

  用以下2種辦法都可以實現查詢。 速度還行。

  幾十萬數據左右, 沒有什麼問題。

  SELECT * FROM `news`

  WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `news`)))

  LIMIT 10;

  SELECT *

  FROM `news` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `news`)-(SELECT MIN(id) FROM `news`))+(SELECT MIN(id) FROM `news`)) AS id) AS t2

  WHERE t1.id >= t2.id

  LIMIT 10;

copyright © 萬盛學電腦網 all rights reserved