1.隨機(jī)讀取連續(xù)多條記錄。經(jīng)過實(shí)踐,可以隨機(jī)讀取多條連續(xù)的數(shù)據(jù)記錄,里頭取值的一般都是主鍵ID來(lái)進(jìn)行最大值、最小值的讀取:
SELECT * FROM example_table AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM example_table)-(SELECT MIN(id) FROM example_table))+(SELECT MIN(id) FROM example_table)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 5;
2.隨機(jī)多條數(shù)據(jù)。以下兩種都是隨機(jī)讀取數(shù)據(jù),查詢數(shù)據(jù)速度、隨機(jī)范圍都基本相差不大
SELECT * FROM example_table WHERE id >= ((SELECT MAX(id) FROM example_table)-(SELECT MIN(id) FROM example_table)) * RAND() + (SELECT MIN(id) FROM example_table) LIMIT 5;
SELECT * FROM example_table WHERE id>=(SELECT floor(RAND() * ((SELECT MAX(id) FROM example_table)-(SELECT MIN(id) FROM example_table)) + (SELECT MIN(id) FROM example_table))) ORDER BY id LIMIT 5;