mysql if函数使用例子

1.场景一

有时查询数量a 同时查询过滤后的数量b

 

2. 代码

SELECT
    count(id) as total_count,
    count(
        IF (
            date(order_time) = DATE(now()),
            1,
            NULL
        )
    ) as today_count
FROM
    passenger_activity_record
where a = #{a,jdbcType=INTEGER}
and b_id= #{bId,jdbcType=INTEGER}

3. 说明

3.1 可以嵌套

3.2 ifnull(condition, expr1)功能类似 当condition为null 则返回expr1 ,否则返回 condition

 

posted @ 2017-11-17 15:21  fangfan  阅读(2880)  评论(0编辑  收藏  举报