简体   繁体   English

如何从最近的三行中获得随机数?

[英]How do I get random from the three latest rows?

I wonder how I can pick one out of the latest three rows from a table. 我想知道如何从表格的最近三行中选择一个。

I have this: 我有这个:

"SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3"

I thought: save the result in an array and random 0-2 and pick from the array, but that wouldn't work since there's many rows. 我以为:将结果保存在一个数组中,并以随机0-2的形式从数组中进行选择,但是由于有很多行,所以这行不通。

如果我理解正确,那么您想从最新的3中随机选择一个。尝试执行子查询:

SELECT * FROM (SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3) t ORDER BY RAND() LIMIT 1
SELECT * FROM
    (SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3) last_three
ORDER BY RAND() LIMIT 1;

SELECT * FROM (SELECT * FROM blog_content ORDER BY id DESC LIMIT 0,3) AS recentBlogs ORDER BY RAND() LIMIT 1

为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM