sql語句給某個字段重新賦值及去除數據庫中的空格


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM