【问题标题】:SQL query: HAVING date = MAX(date) doesn't workSQL 查询:HAVING date = MAX(date) 不起作用
【发布时间】:2015-09-07 09:49:16
【问题描述】:

我尝试编写一个 sql 查询来获取相同 id 的最新日期。所以我写:

select id 
from table 
where id = 10
having table.date = MAX(table.date)

但它仍然返回与刚才相同的结果

select id 
from table 
where id = 10

我不知道为什么,我们不能用这种方式?

谢谢!

【问题讨论】:

    标签: mysql impala


    【解决方案1】:

    你不能在没有分组的情况下使用Having

    试试这个:

    select id 
    from table AS A
    where id = 10 AND table.date = (select MAX(table.date)
                                    from table as B
                                    where a.id = b.id)
    

    【讨论】:

    • 啊,非常感谢.. 但是 MAX 不是关于分组的东西吗?
    • @YilingLiu 不,没必要;)
    • @YilingLiu 如果正确,请将我的答案标记为正确。谢谢
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多