我想知道如何从一张桌子中选出最近三排中的一根。
我有这个:
"SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3"
我想:将结果保存在一个数组中,随机0-2并从数组中选择,但由于行数很多,所以不行。
答案 0 :(得分:2)
如果我理解正确,你想从最新的3中随机选择一个。尝试子查询:
SELECT * FROM (SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3) t ORDER BY RAND() LIMIT 1
答案 1 :(得分:2)
SELECT * FROM
(SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3) last_three
ORDER BY RAND() LIMIT 1;
答案 2 :(得分:1)
SELECT * FROM (SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3) AS recentBlogs ORDER BY RAND() LIMIT 1
为我工作。