慕桂英4014372
2023-06-08 14:36:33
我正在嘗試在單個 sql 中獲取記錄。我想使用可選的 WHERE 子句來做到這一點。這是我試過的 @Query(value = "select * from Products p where type = 'L' and active =
1 and ?1 is null or p.pNum =?1", nativeQuery = true)
public List<Products> findAllParties(String productNumber);這沒有用。當參數為空時,我想帶上所有記錄,否則我想只帶指定的產品。我怎么做?提前致謝。
3 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
制作2個單獨的方法
@Query(value = "select * from Products p where type = 'L' and active =
1 and ?1 is null or p.pNum =?1", nativeQuery = true)
public List<Products> findAllParties(String productNumber);
@Query(value = "select * from Products p ", nativeQuery = true)
public List<Products> findAllParties();

30秒到達戰場
TA貢獻1828條經驗 獲得超6個贊
select * from Products p where req_param is null or ( type='L' and active=1)
因為如果 req_param 為 null 它將對所有行都為真,因此將帶來所有記錄。如果 req_param 不為空/空條件“req_param is null”將為 false,并且僅基于“(type='L' and active=1)”返回行
添加回答
舉報
0/150
提交
取消