【发布时间】: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
我不知道为什么,我们不能用这种方式?
谢谢!
【问题讨论】:
我尝试编写一个 sql 查询来获取相同 id 的最新日期。所以我写:
select id
from table
where id = 10
having table.date = MAX(table.date)
但它仍然返回与刚才相同的结果
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)
【讨论】: