MySql随机化最后10行 [英] MySql randomize the last 10 rows

查看:83
本文介绍了MySql随机化最后10行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关如何随机分配MySql记录的最后10行的帮助.

I need help on how to randomize the last 10 rows of MySql records.

$mysqld = mysql_query(select * from table where amount > amount2 and code = '$code' order by time DESC limit 1);

从上面的语句中,我需要按时间将最后10行随机化,但只能显示1行.

From the statement above I need to randomize the last 10 rows ordered by time but limited only 1 to display.

编辑:换句话说,我需要按时间对表进行排序,然后将重点放在最后10行.从这最后的10行中,我需要选择一个,并且它必须是随机的,我会得到这一行.

EDIT: In other words, I need to have the table ordered by time and then I need to focus on the last 10 rows. From these last 10 rows, I need to pick one and it must be random, which one I get.

这可能吗?

谢谢

推荐答案

假定time是插入记录的时间,那么将从表中获取最新的10行:

Assuming that time is the time when record was inserted, this will get you the latest 10 rows from the table:

SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
  ORDER BY `time` DESC LIMIT 10

现在,您可以将结果用作临时表,对其进行随机排序(因为它只有10行)并返回一行:

Now, you can use the result as a temporary table, sort it randomly (as it's only 10 rows) and return one row:

SELECT * FROM (
  SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
    ORDER BY `time` DESC LIMIT 10
) AS temptable 
ORDER BY RAND()
LIMIT 1

这篇关于MySql随机化最后10行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆