- 官方说明:
在 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 = #{参数}的数据
sql中having子句的使用理解
最新推荐文章于 2023-06-18 12:07:08 发布