Mysql选择最后添加的5个并使它们变为rand

时间:2013-07-26 23:08:03

标签: php mysql

我要尝试做的是按照查看时间选择最后5个顺序,从中选择5个以制作兰特并给我1

例如,如果我选择viewlast:56789,56788,56787,56786,56785给我兰特这5个数字1,让我们说这个56788或者这个56786

更新

$Last_Video         = $db->fetch_all("select VID, thumb FROM video WHERE title LIKE '%" . $Channel['name'] . "%' ORDER BY viewtime DESC limit 5");

这是我之前使用的命令

2 个答案:

答案 0 :(得分:3)

取最后5个然后按RAND()命令:

$query = '
    SELECT VID, thumb
    FROM video
    WHERE id IN (
        SELECT VID
        FROM video
        WHERE title LIKE "%'.$Channel['name'].'%"
        ORDER BY viewtime DESC
        LIMIT 5)
    ORDER BY RAND()
    LIMIT 1
';

答案 1 :(得分:1)

它可能会对您有所帮助:

SELECT * 
FROM (
    SELECT * FROM <#TABLE> ORDER BY viewtime DESC LIMIT 5
) AS TBL 
ORDER BY rand() LIMIT 1

使用表名更改<#TABLE>