<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 java
IFNULL(SUM(integral_value),0)
2:時間區間date
and create_time > #{dateStart} and create_time < #{dateEnd}
3:查詢運算select
((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}))