比如有个表:Student
select * from Student where updater=ifnull(?,updater)
如果传入参数是NULL的话,我的认为是全部查询出来。
但事实上如果updater有值的可以查到,updater是空的就查不到。
因为查询可能有很参数,有的有值有的可能是空。
用prepareStatement。
所以很多地方用到类似updater=ifnull(?,updater)
我没有用hibernate,spring jdbc,ibatis等。
谁知道帮忙解决一下。
我上面回答你了,你没试阿~~
[code="sql"]
-- ?是参数,当参数是null的时候检索所有的数据,不是null的时候,检索满足条件的数据
select * from Student where updater=? or ? is null
[/code]
传的是null,只会查找出updater是null的。
select * from Student 取消限定条件这样查出来的才是全部嘛
[code="sql"]
select * from Student where updater=? or ? is null
[/code]
直接用你给的sql结合prepareStatement可以完成你的需求,用prepareStatement设置参数值的时候最好用setObject
mysql中'' 和 null不一样
[code="sql"]
-- 会把updater=''的数据检索出来。
select * from Student where updater=''
--检索不到数据,判断是否为null要通过is null
select * from Student where updater=null
--检索updater是null的数据
select * from Student where updater is null
--如果表中数据有null,则这条记录不会检索出来。
select * from Student where updater=updater
[/code]