MySQL 的IFNULL()、ISNULL()和NULLIF()函数
一、IFNULL(expr1,expr2)用法
假如expr1不为NULL,则 IFNULL() 的返回值为expr1; 否则其返回值为 expr2。IFNULL()的返回值是数字或是字符串,具体情况取决于其所使用的语境。
mysql> SELECT IFNULL(1,0);
-> 1
mysql> SELECT IFNULL(NULL,10);
-> 10
mysql> SELECT IFNULL(1/0,10);
-> 10
mysql> SELECT IFNULL(1/0,'yes');
-> 'yes'
列子
<!-- 判断库存数量 -->
<select id="checkStock" resultType="java.lang.Integer" >
SELECT (0) FROM t_package_meals_price a
<where>
<if test="saleDate != null and saleDate !=''">
AND a.sale_date = DATE_FORMAT(#{saleDate}, '%Y-%m-%d')
</if>
<if test="mealId != null and mealId != ''">
AND a.meal_id = #{mealId}
</if>
<if test="personTypeId != null and personTypeId != ''">
AND a.person_type_id = #{personTypeId}
</if>
<choose>
<when test="isPart == 0"><!-- 直客 -->
AND a.sale_sum_amount - IFNULL(a.sale_reserve_amount,0) >= #{num}
</when>
<when test="isPart == 1"><!-- 分销 -->
AND a.part_sum_amount - IFNULL(a.part_reserve_amount,0) >= #{num}
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
还没有评论,来说两句吧...