如何编写一个SQL语句,使其仅在参数有值的情况下在where子句中包含条件(我仅希望在参数有有效值的情况下在where子句中包含field2检查)
SELECT * FROM abc
WHERE if(:param1 has a value that is not null or white space) then field1 = :param1
AND if(:param2 has a value that is not null or white space) then field2 = :param2
AND if(:param3 has a value that is not null or white space) then field3 = :param3
AND if(:param4 has a value that is not null or white space) then field4 = :param4
AND if(:param5 has a value that is not null or white space) then field5 = :param5
1条答案
按热度按时间ibrsph3r1#
典型的方法会使用
or
,并针对参数进行明确的NULL
比较:如果你特别想白色的行为和
NULL
s一样,那么这就变得更复杂了。但是,我认为在传入参数之前将空字符串转换为
NULL
会更常见,因此SQL只将NULL
作为一个特殊值使用。