这个HAVING
子句被添加到 SQL 中是因为WHERE
子句不能与聚合函数一起使用。
聚合函数经常与GROUP BY
条款,并通过添加HAVING
我们可以像我们一样写条件WHERE
条款。
仅列出出现次数超过 5 次的国家/地区:
SELECT COUNT(customer_id), country
FROM customers
GROUP BY country
HAVING COUNT(customer_id) > 5;
运行示例 »
以下 SQL 语句仅列出总价为 400 美元或以上的订单:
SELECT order_details.order_id, SUM(products.price)
FROM order_details
LEFT JOIN products ON order_details.product_id = products.product_id
GROUP BY order_id
HAVING SUM(products.price) > 400.00;
运行示例 »
列出订购金额为 1000 美元或以上的客户:
SELECT customers.customer_name, SUM(products.price)
FROM order_details
LEFT JOIN products ON order_details.product_id = products.product_id
LEFT JOIN orders ON order_details.order_id = orders.order_id
LEFT JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customer_name
HAVING SUM(products.price) > 1000.00;
运行示例 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!