尝试选择具有最高时间戳值和最低 ID 值的 20 个项目?

发布于 2024-11-10 09:40:20 字数 251 浏览 2 评论 0原文

我试图选择具有 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");

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

I am trying to select rows with the 20 highest 'TimeStamp' value, and from those 20 rows the 1 row with the lowest ID value:

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

The above query doesn't work, but it makes sense to me. Is there something wrong with this query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-11-17 09:40:20

错误是#1248 - 每个派生表都必须有自己的别名,这确实是事实。这应该有效:

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

The error is #1248 - Every derived table must have its own alias and this is indeed true. This should work:

$result_last = mysql_query("SELECT * FROM (SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT 20) AS T ORDER BY ID ASC LIMIT 1");
尤怨 2024-11-17 09:40:20

尝试这样的事情:

$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);

Try something like this:

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文