有的時候我們經常需要把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