我想在HQL中编写以下查询:
select v1.maxx, v2.maxx from
(select max(id) maxx from mytable where my_column is not null ) v1,
(select max(id) maxx from mytable where my_column is null) v2;
到目前为止我有以下内容:
DetachedCriteria d1=DetachedCriteria.forClass(MyTable.class, "d1")
.setProjection(Projections.max("id"))
.add(Property.forName("myColumn").isNull();
DetachedCriteria d2=DetachedCriteria.forClass(MyTable.class, "d2")
.setProjection(Projections.max("id"))
.add(Property.forName("myColumn").isNotNull();
但是我很难将它们组合成一个标准对象。
答案 0 :(得分:0)
以下是我将两个子查询组合在一起的方法:
Criteria criteria = Criteria.forClass(MyTable.class);
criteria.add(Subqueries.propertyEq("v1.maxx", d2));
criteria.add(Subqueries.propertyEq("v2.maxx", d1));
答案 1 :(得分:0)
无法找到如何在SELECT中包含多个子查询。所以我重写了 查询如下,它适用于HQL:
select max(k.id), max(l.id) from myTable k, myTable l
where k.myColumn is not null and l.myColumn is null;