MySQL select count(*) 语句不包括最小值

发布于 2024-10-22 04:44:09 字数 521 浏览 1 评论 0原文

我有一个简单的 MySQL select 语句:

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ?

但如果最早行的 TIME_STAMP 大于我的 WHERE 阈值,我不想计算第一行/最早行。类似于:

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > $earliest

显然我可以通过额外的先前查询获得最早的 TIME_STAMP 。但我在想是否可以一次完成这一切。我尝试了这个,但抛出了“组函数无效使用”错误:

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > MIN(TIME_STAMP)

有什么建议吗?谢谢

I have a simple MySQL select statement:

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ?

But I do not want to count the first/earliest row if the TIME_STAMP for that earliest row is greater than my WHERE threshold. Something like:

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > $earliest

Obviously I can get the earliest TIME_STAMP with an additional prior query. But I was wandering if I can do it all in one. I tried this but throws an "Invalid use of group function" error:

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > MIN(TIME_STAMP)

Any suggestions? Thanks

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

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

发布评论

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

评论(1

苍暮颜 2024-10-29 04:44:09

如果您从嵌套选择中获取了 MIN 值,则您的最后一个查询将起作用。团体功能需要上下文,您至少需要什么?

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > (SELECT   MIN(TIME_STAMP) FROM MY_TABLE)

Your last query would work if you got the MIN value from a nested select. Group functions need context, of what do you want the minimum?

SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > (SELECT   MIN(TIME_STAMP) FROM MY_TABLE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文