目录
总结一下用到的一些函数
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或记录