阅读量:0
HAVE 语句是 SQL 中用于过滤分组后数据的子句,通常与 GROUP BY 和聚合函数一起使用
SELECT column_name(s), aggregate_function(column_name) FROM table_name GROUP BY column_name(s) HAVING condition;
其中:
column_name(s)
:指定要从表中选择的列名。aggregate_function(column_name)
:对选定列应用聚合函数(例如 COUNT、SUM、AVG、MIN 或 MAX)。table_name
:指定查询的表名。GROUP BY column_name(s)
:根据指定的列对结果进行分组。HAVING condition
:设置分组后数据的筛选条件。
以下是一个实际示例,该示例从 “orders” 表中选择 “customer_id” 和每个客户的订单总数,但仅显示订单数量大于 10 的客户:
SELECT customer_id, COUNT(*) as total_orders FROM orders GROUP BY customer_id HAVING total_orders > 10;