站內搜尋
Sunday, March 31, 2013
[MYSQL] 亂數隨機抽出 更有效率的語法 (RAND)
假設我們想隨機抽出三名中獎者,傳統寫法為:
SELECT * ORDER BY RAND( ) LIMIT 3
更有效率的寫法為
SELECT a . * FROM (
SELECT a.no FROM mytable AS a ORDER BY RAND( ) LIMIT 3
) AS a2 INNER JOIN mytable AS a ON a.no = a2.no
速度會比傳統方法快。
參考資料 Q&A 、 mysql亂數取列-比order by RAND()快5倍!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment