试图选择具有最高TimeStamp值和最低ID值的20个项目?

时间:2011-05-26 07:52:46

标签: php mysql phpmyadmin

我正在尝试选择具有20个最高'TimeStamp'值的行,从这20行中选择具有最低ID值的1行:

$result_last = mysql_query("SELECT * FROM (SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT 20) ORDER BY ID ASC LIMIT 1");

以上查询不起作用,但对我来说很有意义。这个查询有问题吗?

2 个答案:

答案 0 :(得分:2)

错误是#1248 - Every derived table must have its own alias,确实如此。这应该有效:

$result_last = mysql_query("SELECT * FROM (SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT 20) AS T ORDER BY ID ASC LIMIT 1");

答案 1 :(得分:-2)

尝试这样的事情:

$result_last = mysql_query("SELECT * FROM Events WHERE TimeStamp IN(SELECT TimeStamp FROM Events ORDER BY TimeStamp DESC LIMIT 10") ORDER BY TimeStamp ASC LIMIT 1);