sql中having子句的使用理解

1 篇文章 0 订阅
  • 官方说明:

    在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。

    HAVING 子句可以让我们筛选分组后的各组数据。比如:

    SELECT 
        aggregate_function(column_name)
    FROM 
        table_name
    WHERE 
        column_name operator value
    GROUP BY 
        column_name
    HAVING 
        aggregate_function(column_name) operator value;
  • 个人理解: where子句对源表字段进行筛选,而having子句对Select子句的得到的结果字段作进一步筛选

    select
                t1.post_id as postId,
                t1.post_type as postType,
                t1.post_name as postName,
                CASE WHEN
                    ISNULL(t2.perf_formula) THEN 0 ELSE 1
                END AS status
            from
                ew_work_post t1
            left join
                salary_perf t2
            on
                t1.post_id = t2.post_id
            where
                t1.status = 0
                <if test="postType != null and postType != ''"> and t1.post_type = #{postType}</if>
                <if test="postName != null and postName != ''"> and t1.post_name = #{postName}</if>
            <if test="status != null">
                having status = #{status}
            </if>
            order by
                t1.post_type, t1.post_level asc

    如上代码,where子句中,t1.status = 0,对t1表中status字段进行筛选,得到status=0的记录。而having子句中对select出的结果表的status字段进行筛选,得到最终结果status = #{参数}的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值