总结mysql的一些函数用法

目录

1、case when then else end 

2、GROUP_CONCAT:字符串拼接

3、IFNULL :null值替换

4、FIND_IN_SET


总结一下用到的一些函数

1、case when then else end 

<update id="updateConcernedFlag" parameterType="com.example.supportjsp.entity.ProblemRecord">
  update problem_record
  set
      concerned_flag = (CASE concerned_flag WHEN 0 THEN 1 WHEN 1 THEN 0 END),
      updated_name = #{updatedName,jdbcType=VARCHAR},
      updated_time =NOW()
  where id = #{id,jdbcType=INTEGER}
</update>

解释:当concerned_flag是0的时候修改为1,是1 的时候修改为0

2、GROUP_CONCAT:字符串拼接

SELECT GROUP_CONCAT(role_name) FROM `sys_role`

解释:把role_name字段值用逗号,拼接在一起。结果类似:总管理员,系统维护管理员

group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])

3、IFNULL :null值替换

select IFNULL(updated_name,"测试数据") from sys_notices

解释:把updated_name 字段结果是null的替换成 "测试数据"。

IFNULL(expr1,expr2)

如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值

4、FIND_IN_SET

<select id="queryByUserNameAndPassword" parameterType="com.example.supportjsp.entity.SysUser"
        resultMap="SysUserVoMap">
    select
      user_id,user_role_id, user_name, user_account, `password`, `memo`, created_name, created_time, sort, source,dept_code, login_type,deleted_flag
    from sys_user
    where  deleted_flag=0
    and user_account = #{userAccount} and password = #{password} and FIND_IN_SET(#{deptCode},dept_code) limit 0,1
</select>

解释:数据库中存入的deptCode为[10001,10003] ,如果想查是否包含10001是否存在可以用

FIND_IN_SET(str,strlist)
str 要查询的字符串
strlist 字段名 参数以”,”分隔 如 (1,2,6,8,10,22)
查询字段(strlist)中包含(str)的结果,返回结果为null或记录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值