<select id="countAssetTab" resultType="Integer" parameterType="HashMap">
SELECT COUNT(1) FROM TTRD_ASSET_MANAGE_PLAN_EXTEND
<where>
<if test="i_code != null and i_code !=''">
AND (I_CODE = #{i_code}
OR I_CODE = CONCAT(#{i_code},'(temp)'))
</if>
<if test="a_type != null and a_type !=''">
AND A_TYPE = #{a_type}
</if>
AND ZMZC_FLAG = 1
</where>
</select>
如上,是我在公司項目中寫的代碼,我遇到的問題是,在
AND (I_CODE = #{i_code}
OR I_CODE = CONCAT(#{i_code},'(temp)'))
代碼部分我第一次沒有應用括號的情況下,查找出來的數據是不正確的,因爲在sql語句中的執行順序是not>and>or的,
因此在or的部分沒有使用括號的情況下,將當前的語句變成了
AND (I_CODE = #{i_code})
OR (I_CODE = CONCAT(#{i_code},'(temp)')
AND A_TYPE = #{a_type}
AND ZMZC_FLAG = 1)
所以導致一直查找到的數據不正確。