[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
简介:
HAVING COUNT() 是 SQL 中用于筛选分组结果的关键字,它通常与 GROUP BY 一起使用。HAVING COUNT()
的作用是对分组后的结果进行过滤,只保留满足条件的分组结果。
在没有分组的情况下having和Where 类似。有分组的时候 Where对分组前内容过滤,having是分组后的内容进行筛选。having
里面可以写聚合函数。
说明:
主表 CUSTOM_CABINETS 订单表
副表 CUSTOM_CABINETS_BRAND 品牌表
一、关联查询全部数据
注意: 查询两表相同的数据用inner join,我这里就用left join 为了复现问题。
二、关联查询 加Group by 分组
对品牌名称 主表.BRAND_NAME 和 品牌编码 副表.BRAND_CODE 进行分组后 ,查询后有一行空数据,说明我主表数据
存在没有品牌编码(编码 对应 品牌名称)的行,所以现在看到 分组后的数据有NULL。
主表统计一下count(a.BRAND_CODE) ,为了更清晰些。
三、关联查询 加 HAVING COUNT 函数
注意重点: group by 分组后 在用having count(*) > 0 进行筛选查询
通过 HAVING COUNT(a.BRAND_CODE) > 0 筛选 表中想要的数据 排除NULL 。
总结:
* ①使用 INNER JOIN 将两个表连接起来,连接条件为需要进行计数的列。 SELECT column1, column2, ...,
COUNT(column_name) FROM table1 INNER JOIN table2 ON table1.column_name =
table2.column_name GROUP BY column1, column2, ... HAVING COUNT(column_name) > 0;
* ②在 GROUP BY 子句中指定需要进行计数的列,并使用 COUNT() 函数对这些列进行计数。 SELECT column1, column2,
..., COUNT(column_name) FROM table1 GROUP BY column1, column2, ... HAVING
COUNT(column_name) > 0;
* ③在 HAVING COUNT() 子句中指定计数条件,只保留计数结果大于等于 0 的分组结果。 SELECT column1, column2,
..., COUNT(column_name) FROM table1 GROUP BY column1, column2, ... HAVING
COUNT(column_name) > 0;