是否可以编写与此SQL相当的Querydsl?
SELECT coalesce(m.MATERIAL_GROUP, sc.MATERIAL_ID) AS GROUP_ID FROM STOCK_CARD sc JOIN MATERIAL m ON (sc.MATERIAL_ID = m.ID) GROUP BY GROUP_ID;
在Querydsl中我尝试过(stockCard是静态导入的):
public List<Long> justAnExample() {
NumberPath<Long> group_id = Expressions.numberPath(Long.class, "group_id");
return from(stockCard)
.groupBy(group_id)
.list(stockCard.material.materialGroup.id.coalesce(stockCard.material.id).as(group_id));
}
但似乎Querydsl忽略了“as()”,因为生成的SQL是:
select coalesce(material1_.MATERIAL_GROUP, stockcard0_.MATERIAL_ID) as col_0_0_ from STOCK_CARD stockcard0_, MATERIAL material1_ where stockcard0_.MATERIAL_ID=material1_.id group by group_id
请注意,这只是一个非常简化的查询,可以尝试是否可行。 谢谢!