mybatis-sql條件判斷 以及 參數有無決定是否關聯表

select * from where
<if test="KHLX_DM !=null and KHLX_DM !=''">
            <choose>
                <when test="KHLX_DM==0">
                    AND  KHLX_DM!=10
                    AND  KHLX_DM!=11
                    AND  KHLX_DM!=12
                </when>
                <otherwise>
                    AND  KHLX_DM = #{KHLX_DM}
                </otherwise>
            </choose>
        </if>

正如上述代碼所述, 當出現一個條件不爲空下:對條件繼續進行下一層判斷

select *
FROM 
    HTGL_HTDJ A
     <if test="YW_ZZJG_DM !=null and YW_ZZJG_DM !=''">,PT_DM_ZZJG P</if>
WHERE DEL_BZ = 'N' AND VIP_HT_BS=2 and YYGLZX_BZ=1 and (SJZT=0 or (SJZT=2 and HSBZ='1'))
            
<if test="YW_ZZJG_DM !=null and YW_ZZJG_DM !=''" >
               AND A.YW_ZZJG_DM=P.ZZJG_DM
</if>

mybatis應用真是千變萬化,根據參數的有無來決定是否關聯表,如上sql所示

  select
      *,
    concat(SFJC,HTHM) HTHM
  from HTGL_ZXGM_DD zxdd
  where
      zxdd.SFJC = (
            select case
            when substr(ZZJG_MC, 0, 2) = '河南' then 'HA'
            when substr(ZZJG_MC, 0, 2) = '甘肅' then 'GS'
            when substr(ZZJG_MC, 0, 2) = '廣西' then 'GX'
            when substr(ZZJG_MC, 0, 2) = '新疆' then 'XJ'
            when substr(ZZJG_MC, 0, 2) = '青海' then 'QH'
            when substr(ZZJG_MC, 0, 2) = '北京' then 'BJ'
            when substr(ZZJG_MC, 0, 2) = '天津' then 'TJ'
            when substr(ZZJG_MC, 0, 2) = '河北' then 'HE'
            when substr(ZZJG_MC, 0, 2) = '遼寧' then 'LN'
            when substr(ZZJG_MC, 0, 2) = '吉林' then 'JL'
            when substr(ZZJG_MC, 0, 2) = '山東' then 'SD'
            when substr(ZZJG_MC, 0, 2) = '江蘇' then 'JS'
            when substr(ZZJG_MC, 0, 2) = '上海' then 'SH'
            when substr(ZZJG_MC, 0, 2) = '浙江' then 'ZJ'
            when substr(ZZJG_MC, 0, 2) = '安徽' then 'AH'
            when substr(ZZJG_MC, 0, 2) = '福建' then 'FJ'
            when substr(ZZJG_MC, 0, 2) = '江西' then 'JX'
            when substr(ZZJG_MC, 0, 2) = '廣東' then 'GD'
            ELSE '其他' END SFJC
            from PT_DM_ZZJG
            where ZZJG_DM = F_ROOT_ZZJG(#{zzjgDm})  
)

以上是對在mybatis中關於substr函數,以及case  when 的運用擴展;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章