MySQL 的IFNULL()、ISNULL()和NULLIF()函数

桃扇骨 2023-10-17 18:28 96阅读 0赞

MySQL 的IFNULL()、ISNULL()和NULLIF()函数

一、IFNULL(expr1,expr2)用法

  1. 假如expr1不为NULL,则 IFNULL() 的返回值为expr1; 否则其返回值为 expr2IFNULL()的返回值是数字或是字符串,具体情况取决于其所使用的语境。
  2. mysql> SELECT IFNULL(1,0);
  3. -> 1
  4. mysql> SELECT IFNULL(NULL,10);
  5. -> 10
  6. mysql> SELECT IFNULL(1/0,10);
  7. -> 10
  8. mysql> SELECT IFNULL(1/0,'yes');
  9. -> 'yes'
列子
  1. <!-- 判断库存数量 -->
  2. <select id="checkStock" resultType="java.lang.Integer" >
  3. SELECT (0) FROM t_package_meals_price a
  4. <where>
  5. <if test="saleDate != null and saleDate !=''">
  6. AND a.sale_date = DATE_FORMAT(#{saleDate}, '%Y-%m-%d')
  7. </if>
  8. <if test="mealId != null and mealId != ''">
  9. AND a.meal_id = #{mealId}
  10. </if>
  11. <if test="personTypeId != null and personTypeId != ''">
  12. AND a.person_type_id = #{personTypeId}
  13. </if>
  14. <choose>
  15. <when test="isPart == 0"><!-- 直客 -->
  16. AND a.sale_sum_amount - IFNULL(a.sale_reserve_amount,0) >= #{num}
  17. </when>
  18. <when test="isPart == 1"><!-- 分销 -->
  19. AND a.part_sum_amount - IFNULL(a.part_reserve_amount,0) >= #{num}
  20. </when>
  21. <otherwise>
  22. </otherwise>
  23. </choose>
  24. </where>
  25. </select>

发表评论

表情:
评论列表 (有 0 条评论,96人围观)

还没有评论,来说两句吧...

相关阅读