假设我有一个由4列组成的表(Product),last_update是DATE字段。
id prod_id last_update status
1 100 7/8/2014 built
2 100 9/10/2014 in process
3 210 7/8/2014 in process
4 210 9/10/2014 built
是否可以在hibernate中编写查询以选择每个产品的最后更新行?在这种情况下,id 2和4?
一如既往地欣赏你的答案
答案 0 :(得分:0)
尝试:
select *
from my_table t1
where last_update = (select max(last_update) from my_table t2 where t1.prod_id = t2.prod_id;
具体的Hibernate: 来自similar question:
select p1 from my_table t1 where
t1.last_update = max (
select t2.last_update from my_table t2 where
t2.prod_id=t1.prod_id
)