【发布时间】:2020-07-06 14:54:21
【问题描述】:
我试图用这个查询实现的是:总是做“母亲”的条件,但如果没有母亲,那么做“爸爸”的条件,这可能吗?
"select name from Person where idParents = :idParents and"
+ " CASE " +
" WHEN idMother !=NULL THEN idMother = :idMother " +
" ELSE idFather = :idFather " +
" END";
【问题讨论】:
-
但是 HQL 呢?我不能用吗?那么你将如何实现 if 语句
-
我的意思是...人有名字,idMother 和 idFather,我只希望“idFather = :idFather”条件在没有 idMother 时工作,如果有 idMother,我不希望“父亲”条件起作用。
-
@SimonMartinelli CASE 可用于标准 SQL 的 WHERE 子句中。
-
哦,你是对的。对不起
-
@Martinez 如果 idMother 不为空但不等于 :idMother 你想要基于 idFather 的连接吗?或者如果 idMother 为空,仅会使用 idFather?