就我而言,只有前 2 个参数是强制性的。当用户没有给出其余参数时,我将使用字符“%”作为 LIKE 语句,以便显示所有数据。知道如何解决 IN 语句的这个问题吗?解决方案参数 IN ('%') 不起作用 => Null
参数:
java.sql.Timestamp、java.sql.Timestamp、java.lang.String、java.lang.String、java.lang.String、java.lang.String
查询:
SELECT *
FROM RC_Recall t INNER JOIN RC_RECALLSTORE s ON t.ID = s.RECALLID
WHERE t.creationdate >= ?1 AND t.enddate <= ?2 AND t.id LIKE ?3
AND t.afdeling IN (?4) AND s.storeid IN (?5)
ORDER BY ID DESC
最佳答案
SQL 中的 IN 语句不能接受通配符。您必须在查询中添加 OR 才能使其正常工作...
select *
from RC_Recall t INNER JOIN RC_RECALLSTORE s ON t.ID = s.RECALLID
where t.creationdate >= ?1
AND t.enddate <= ?2
AND t.id LIKE ?3
AND ('%' = ?4 OR t.afdeling IN (?4))
AND ('%' = ?5 OR s.storeid IN (?5))
ORDER BY ID DESC
如果您使用参数位置来传递参数,则可能需要将其更改为
AND ('%' = ?4 OR t.afdeling IN (?5))
AND ('%' = ?6 OR s.storeid IN (?7))
希望有帮助
关于java - SQL用IN语句选择所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506515/