SQL 查询:HAVING date = MAX(date) 无效

SQL query: HAVING date = MAX(date) doesn't work

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

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

但它仍然returns我得到了与

相同的结果
select id 
from table 
where id = 10

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

谢谢!

您不能在没有分组的情况下使用 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)