1. 程式人生 > >SQL NULL賦值0 時間區間 查詢運算

SQL NULL賦值0 時間區間 查詢運算

<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 &gt; #{dateStart} and create_time &lt; #{dateEnd})
    -
    (select IFNULL(SUM(integral_value),0)
    from integral WHERE type LIKE 1 and user_id LIKE #{userId}
    and create_time &gt; #{dateStart} and create_time &lt; #{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 &gt; #{dateStart} and create_time &lt; #{dateEnd}

3:查詢運算

((select IFNULL(SUM(integral_value),0)
from integral WHERE type LIKE 0 and user_id LIKE #{userId}
and create_time &gt; #{dateStart} and create_time &lt; #{dateEnd})
-
(select IFNULL(SUM(integral_value),0)
from integral WHERE type LIKE 1 and user_id LIKE #{userId}
and create_time &gt; #{dateStart} and create_time &a