我有一个简单的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)
有什么建议吗?感谢
答案 0 :(得分:2)
如果您从嵌套选择中获得MIN
值,那么您的上一个查询将起作用。组功能需要上下文,您想要的最小值是什么?
SELECT COUNT(*) AS COUNT FROM MY_TABLE WHERE TIME_STAMP > ? AND TIME_STAMP > (SELECT MIN(TIME_STAMP) FROM MY_TABLE)