MySQL select count(*) 语句不包括最小值
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从嵌套选择中获取了
MIN
值,则您的最后一个查询将起作用。团体功能需要上下文,您至少需要什么?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?