SQL NULL賦值0 時間區間 查詢運算
阿新 • • 發佈:2019-08-11
<select id="programGetIntegral" parameterType="java.util.Map" resultMap="BaseResultMap"> SELECT user_id, ((select IFNULL(SUM(integral_value),0) from integral WHERE type LIKE 0 and user_id LIKE #{userId} and create_time > #{dateStart} and create_time < #{dateEnd}) - (select IFNULL(SUM(integral_value),0) from integral WHERE type LIKE 1 and user_id LIKE #{userId} and create_time > #{dateStart} and create_time < #{dateEnd})) as 'integral_value' from integral WHERE user_id LIKE #{userId} group by user_id </select>
1:NULL賦值0
IFNULL(SUM(integral_value),0)
2:時間區間
and create_time > #{dateStart} and create_time < #{dateEnd}
3:查詢運算
((select IFNULL(SUM(integral_value),0) from integral WHERE type LIKE 0 and user_id LIKE #{userId} and create_time > #{dateStart} and create_time < #{dateEnd}) - (select IFNULL(SUM(integral_value),0) from integral WHERE type LIKE 1 and user_id LIKE #{userId} and create_time > #{dateStart} and create_time &a