使用 JPA 在 Sql 中具有不同值的多个语句 where 子句
Multiples statements where clause with differents values in Sql using JPA
大家好,我正在使用 JPA 为我的项目执行本机查询,但我不知道如何执行 if 和 else
在 where 子句中,我的 ?0 可以是 4 个值:10、20、30 和 40,当 ?0 是 10、20、30 时,我应该使用 CI.QTD <=
当 ?0 为 40 时,条件应为 >= 。我怎样才能做到这一点?
在一个条件下遵循我的代码:
SELECT rownum AS id,
x.*
FROM (
SELECT ci.text,
ci.lenght
FROM ci_table CI
WHERE (?0 IS NULL OR ci.qtd <= ?0)
GROUP BY ci.text,
ci.lenght
ORDER BY ci.text) x ,
nativequery = true);
调整后添加查询:
@Query(value =
"SELECT rownum as ID, X.* FROM (SELECT " +
" CI.PARAM1 " +
" CI.PARAM2, " +
" CI.PARAM3, " +
" CI.PARAM4, " +
" CI.PARAM5" +
" FROM " +
" CM_PARAMS CI " +
" WHERE " +
" (?1 is null or CI.PARAM6 = ?1) " +
" AND (?2 is null or CI.PARAM7 = ?2) " +
" AND (?3 is null or CI.PARAM8 = ?3) " +
" AND (?4 is null or CI.PARAM9 = ?4) " +
" AND (?5 is null or CI.PARAM10 = ?5) " +
" AND (?6 is null or CI.PARAM11 = ?6) " +
" AND (?7 is null or CI.PARAM12 = ?7) " +
" AND (?8 is null or CI.PARAM13 = ?8) " +
" AND (CASE WHEN ?9 >30 THEN CI.PARAM14 > 30 " +
" CASE WHEN ?9 <31 THEN CI.PARAM15 <= ?9 END) = 1 " +
" GROUP BY " +
" CI.PARAM1, " +
" CI.PARAM2, " +
" CI.PARAM3, " +
" CI.PARAM4, " +
" CI.PARAM5" +
" ORDER BY " +
" CI.PARAM1 ASC) X " , nativeQuery = true)
尝试:
SELECT rownum AS id,
x.*
FROM (
SELECT ci.text,
ci.lenght
FROM ci_table CI
WHERE (CASE WHEN ?0 IN (10,20,30) THEN CI.QTD <= ?0
WHEN ?0 = 40 THEN CI.QTD > ?0 END) = 1
GROUP BY ci.text,
ci.lenght
ORDER BY ci.text) x ,
nativequery = true);
大家好,我正在使用 JPA 为我的项目执行本机查询,但我不知道如何执行 if 和 else 在 where 子句中,我的 ?0 可以是 4 个值:10、20、30 和 40,当 ?0 是 10、20、30 时,我应该使用 CI.QTD <= 当 ?0 为 40 时,条件应为 >= 。我怎样才能做到这一点?
在一个条件下遵循我的代码:
SELECT rownum AS id,
x.*
FROM (
SELECT ci.text,
ci.lenght
FROM ci_table CI
WHERE (?0 IS NULL OR ci.qtd <= ?0)
GROUP BY ci.text,
ci.lenght
ORDER BY ci.text) x ,
nativequery = true);
调整后添加查询:
@Query(value =
"SELECT rownum as ID, X.* FROM (SELECT " +
" CI.PARAM1 " +
" CI.PARAM2, " +
" CI.PARAM3, " +
" CI.PARAM4, " +
" CI.PARAM5" +
" FROM " +
" CM_PARAMS CI " +
" WHERE " +
" (?1 is null or CI.PARAM6 = ?1) " +
" AND (?2 is null or CI.PARAM7 = ?2) " +
" AND (?3 is null or CI.PARAM8 = ?3) " +
" AND (?4 is null or CI.PARAM9 = ?4) " +
" AND (?5 is null or CI.PARAM10 = ?5) " +
" AND (?6 is null or CI.PARAM11 = ?6) " +
" AND (?7 is null or CI.PARAM12 = ?7) " +
" AND (?8 is null or CI.PARAM13 = ?8) " +
" AND (CASE WHEN ?9 >30 THEN CI.PARAM14 > 30 " +
" CASE WHEN ?9 <31 THEN CI.PARAM15 <= ?9 END) = 1 " +
" GROUP BY " +
" CI.PARAM1, " +
" CI.PARAM2, " +
" CI.PARAM3, " +
" CI.PARAM4, " +
" CI.PARAM5" +
" ORDER BY " +
" CI.PARAM1 ASC) X " , nativeQuery = true)
尝试:
SELECT rownum AS id,
x.*
FROM (
SELECT ci.text,
ci.lenght
FROM ci_table CI
WHERE (CASE WHEN ?0 IN (10,20,30) THEN CI.QTD <= ?0
WHEN ?0 = 40 THEN CI.QTD > ?0 END) = 1
GROUP BY ci.text,
ci.lenght
ORDER BY ci.text) x ,
nativequery = true);