【发布时间】:2012-03-28 12:49:43
【问题描述】:
我有以下表格:
SN、D 和 C 有以下链接:
SN 包含一个 FK 到 D (d_id) (ManyToOne),SN 包含一个 FK 到 C (c_id)(OneToOne),C 有一个 FK 到 D (d_id, OneToOne)。 D 还包含(在其他列中,一个字符串 number 和一个字符串 name)。
现在,我有一个这样的 HQL 查询:"from SN sn where (sn.d is not null or sn.c.d is not null)"(因此,如果 sn.d 为空,则从 sn.c.d 中获取 d)。不过,这个集合应该可以按 d.number 或 d.name 排序。因为在查询创建时我们不知道按哪个“d”排序,所以我尝试过这样的:
"from SN sn where (sn.d is not null or sn.c.d is not null) order by coalesce (sn.d.name, sn.c.d.name)" 但它不起作用,我得到的结果比正常选择少;我也尝试了"order by case when sn.d is not null then sn.d.name else sn.c.d.name",但结果相同。
如果 HQL 中第一列为空,我如何按另一列排序?
谢谢
【问题讨论】:
标签: sql hibernate sql-order-by hql