SELECT * FROM tbl AS t1 INNER JOIN ( SELECT ROUND(RAND() * (( SELECT MAX(id) FROM tbl ) - ( SELECT MIN(id) FROM tbl )) + ( SELECT MIN(id) FROM tbl )) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1
1.假设你每次需要随机50篇文章。 2.让程序随机出100个不重复ID,之所以是100个是防止某些文章删除了,可根据情况调整。 3.用select * from post where id in (2,3,5,7,11...) limit 50;取出文章。 4.好了。即使你运气比较差,随机的100个ID里面有60篇文章被删除了,那就随他去吧,who care?