$MysqLd = MysqL_query(select * from table where amount > amount2 and code = '$code' order by time DESC limit 1);
从上面的陈述我需要随机排列按时间排序的最后10行,但仅限于1显示.
编辑:换句话说,我需要按时间排序表,然后我需要关注最后10行.从这最后10行,我需要选择一行,它必须是随机的,我得到的是哪一行.
这可能吗?
谢谢
最佳答案
假设时间是插入记录的时间,这将从表中获取最新的10行:
原文链接:https://www.f2er.com/mysql/433377.htmlSELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
现在,您可以将结果用作临时表,随机排序(因为它只有10行)并返回一行:
SELECT * FROM (
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
) AS temptable
ORDER BY RAND()
LIMIT 1