有的时候我们经常需要把sql中的某个字段换成想要的值
或者说当某个字段为空值赋值为0或true
写一个简单的例子
第一种写法
select name,ifnull(age,'0'),adress from user
第二种写法
select name,case when age is null then 1 else age end,adress from user
第三种写法
select name,#{需要的字段及mapper传入的值},age from 表名
mybatis种的判断
<choose> <when test=""> //... </when> <otherwise> //... </otherwise> </choose>
<choose> <when test="xxx!= null and xxx !=''"> and name in <foreach collection="names" item="name" index="index" open="(" close=")" separator=","> #{name} </foreach> </when> <otherwise> and name= '' </otherwise> </choose>
sql中去掉换行和空格
replace(replace(字段名,char(10),''), CHAR(13) ,'')
select replace(name,char(10),'') as name from user where age =18