####select * from article if( typeCode != null && typeCode != '')where TYPE_CODE like CONCAT("", #{typeCode},"%")
####if后括号为真时执行where,if后括号为假时只执行select * from article
####正确的写法应该是怎样的?
IF 表达式
IF( expr1 , expr2 , expr3 )
expr1 的值为 TRUE,则返回值为 expr2
expr1 的值为FALSE,则返回值为 expr3
你将if写在where后面,expr1换成你的判断条件,expr2换成你的查询条件,expr3换成1=1(如果expr1不成立,自行select * from 表名 where 1=1)
望采纳!
参考:https://www.cnblogs.com/baizhanshi/p/9284782.html
select * from article
where
if(isnull(typeCode,'')='', 1=1, TYPE_CODE like CONCAT("", #{typeCode},"%"))
我觉得你这个sql最大的问题还不是if语法怎么用,你if里面的参数来源根本就不是你from后面的表,所以你如果
想根据别处的信息来决定select语句的where条件,那肯定是不能用sql来实现的,你得考虑别的方法了。
if语句是来判断你from的表里的字段是否符合要求,来展示对应的转换数据,记住,它的参数都是来源你from的表。
这里你把if直接干掉,用java或者python,或者别的什么语言来实现你描述的if的目的。
还有就是你拼接函数concat里面的参数,是字符串的话加上单引号,字段的话不需要加,就是'#{typeCode}'
你里面的且关系&&,改成and,如果是或关系,就用or。
https://www.cnblogs.com/lwx521/p/10488527.html
select * from article where typeCode is null or typeCode = ''
union
select * from article where typeCode is not null and typeCode != '' and TYPE_CODE like CONCAT("", #{typeCode},"%");