纯SQL解决方案当传入某个值为查询条件,非空则按条件查询,条件值为空则不使用该条件查询
(初版:仅供参考,暂不考虑SQL优化)
sql如下:
select * from 表名 t
where -- 查询条件 当值为非空时,结果为1,反之为0
case when t.列名= '值' then (case when '值' is not null then '1' else '0' end)
-- 查询条件 当值非空时,结果为0,反之为1
else (case when '值' is not null then '0' else '1' end)
end ='1'
结果如下图(为空时)
如下图(非空时)