>Mybatis CASE WHEN 的用法
Case具有两种格式。简单Case函数和Case搜索函数。
简单Case函数
CASE sex
WHEN ‘1’ THEN ‘男’
WHEN ‘2’ THEN ‘女’
ELSE ‘其他’ END
–Case搜索函数
CASE WHEN sex = ‘1’ THEN ‘男’
WHEN sex = ‘2’ THEN ‘女’
ELSE ‘其他’ END
两 种方式,可以实现相同的功能。简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式。还有一个需要注意的问题,Case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略。
–比如说,下面这段SQL,你永远无法得到“第二类”这个结果
CASE WHEN col_1 IN ( ‘a’, ‘b’) THEN ‘第一类’
WHEN col_1 IN (‘a’) THEN ‘第二类’
ELSE’其他’ END
注意: 改完sql,也要改返回值类型呀,不然会报如下错误
对应不上字段,查是能查了,但是匹配字段的时候报错!!!
ERROR 5596 --- [nio-9099-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'push_manner' from result set. Cause: java.sql.SQLDataException: Cannot determine value type from string '站内信'
; Cannot determine value type from string '站内信'; nested exception is java.sql.SQLDataException: Cannot determine value type from string '站内信'] with root cause
com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from string '站内信'
实例:
<select id="getOutStockApply" parameterType="com.boot.reservation.entity.domain.qo.OutStockQO"
resultType="com.boot.reservation.entity.outstock.OutStockApplyDTO">
select
id,
apply_out_stock_serial_num applyOutStockSerialNum,
granary_address granaryAddress,
goods_name goodsName,
usable_num usableNum,
goods_level goodsLevel,
out_num outNum,
out_date outDate,
outter_name outterName,
outter_tel outterTel,
outter_id outterId,
out_remark outRemark,
( CASE
WHEN STATUS = '00' THEN '已保存'
WHEN STATUS = '01' THEN '待审核'
WHEN STATUS = '02' THEN '已通过'
WHEN STATUS = '03' THEN '已驳回'
END ) status,
file_id fileId,
pic_id picId
from
storage_out_stock_apply
where
apply_out_stock_serial_num =#{applyOutStockSerialNum}
</select>
相关文章
暂无评论...