Where 1=1 처럼 동적으로 set 컬럼을 바꾸고 싶을때
방법1. Mybatis로 해결 : <set> 사용 (추천)
<set>
<if test="meterChangeUsage != null and meterChangeUsage !=''">
gmp.METER_CHANGE_USAGE = #{meterChangeUsage} ,
</if>
<if test="meterChangeDt != null and meterChangeDt !=''">
gmp.METER_CHANGE_DT = #{meterChangeDt} ,
</if>
<if test="contractMethod != null and contractMethod !=''">
gai.CONTRACT_METHOD = #{contractMethod} ,
</if>
<if test="contractCapacity != null and contractCapacity !=''">
gai.CONTRACT_CAPACITY = #{contractCapacity} ,
</if>
</set>
방법2. 쿼리로 해결 : 바꾸려는 컬럼을 기존 값으로 한 번더 사용 (비추)
set gmp.METER_CHANGE_USAGE = gmp.METER_CHANGE_USAGE
<if test="meterChangeUsage != null and meterChangeUsage !=''">
gmp.METER_CHANGE_USAGE = #{meterChangeUsage} ,
</if>
<if test="meterChangeDt != null and meterChangeDt !=''">
gmp.METER_CHANGE_DT = #{meterChangeDt} ,
</if>
<if test="contractMethod != null and contractMethod !=''">
gai.CONTRACT_METHOD = #{contractMethod} ,
</if>
<if test="contractCapacity != null and contractCapacity !=''">
gai.CONTRACT_CAPACITY = #{contractCapacity} ,
</if>
<set> 사용시 마지막 ,(콤마)는 자동적으로 제거 됨
'개발 > DB' 카테고리의 다른 글
(MariaDB/mySQL) 월별 데이터 값이 가장 많은 날짜 구하기 (= 월별 데이터가 가장 많은 날) (0) | 2022.04.21 |
---|---|
[mySql, mariaDB] case 문 사용 방법 (0) | 2022.04.06 |
(MariaDB) 범위 별로 카운트 구하기 (Group by가 아닌 범위 별 통계) (0) | 2022.01.20 |
(펌) [DBMS] [MySQL] COUNT의 잘못된 인식과 속도차이 (0) | 2022.01.18 |
Oracle 특정일자부터 오늘까지 차이 계산하는 쿼리 (4) | 2021.06.12 |