Mysql 如何在SQL查询中忽略空参数
嗯,我有个问题,我甚至不知道我正在尝试或想做的是可能的还是不可能的 我有一个sql查询:-Mysql 如何在SQL查询中忽略空参数,mysql,Mysql,嗯,我有个问题,我甚至不知道我正在尝试或想做的是可能的还是不可能的 我有一个sql查询:- select * from table where (col1 >= '2016-07-05' and col2 <= '2016-07-07') and col3 = '' and col4 = '' and col5 = '' and col6 = '686486' and col7 = '' and col8 = ''; 选择* 从桌子上 其中(col1>='20
select *
from table
where (col1 >= '2016-07-05' and col2 <= '2016-07-07')
and col3 = ''
and col4 = ''
and col5 = ''
and col6 = '686486'
and col7 = ''
and col8 = '';
选择*
从桌子上
其中(col1>='2016-07-05'和col2如下所示
...
and (@value3 is null or col3 = @value3)
and (@value4 is null or col4 = @value4)
....
我也必须在我的查询中写“@”吗?我从来没有在sql中使用过@。我使用简单的sql,并将其作为嵌入的sql使用java@value只是查询中参数的表示形式。请用搜索字符串替换它。谢谢,唯一的问题是我的愚蠢。否则,答案就完美了。我正在运行您建议的查询,我获取0行,然后我发现我传递的是空字符串,而不是null。这就是问题所在,否则您的解决方案应该是breeze。再次感谢您的帮助。尝试更新您的解决方案,但声誉不足。再次感谢您的好友。祝您编码愉快。。
select *
from table
where (('val1' is null or col1 >= 'val1') and ('val2' is null or col2 <=
'val2'))
and ('val3' is null or col3 = 'val3')
and ('val4' is null or col4 = 'val4')
and ('val5' is null or col5 = 'val5')
and ('val6' is null or col6 = 'val6')
and ('val7' is null or col7 = 'val7')
and ('val8' is null or col8 = 'val8');
...
and (@value3 is null or col3 = @value3)
and (@value4 is null or col4 = @value4)
....