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