Sqlite 当我们已经有WHERE子句时,HAVING子句有什么用?
这里是SQLite的初学者 我是通过在SQLite中使用子句来了解这个组的。我得到了GROUPBY子句的用途。我得到HAVING子句应用于groupby子句返回的组。但是,当我们有WHERE子句时,真的有必要使用它吗 例如: 我有一个名为sales的表,其中包含以下内容:Sqlite 当我们已经有WHERE子句时,HAVING子句有什么用?,sqlite,Sqlite,这里是SQLite的初学者 我是通过在SQLite中使用子句来了解这个组的。我得到了GROUPBY子句的用途。我得到HAVING子句应用于groupby子句返回的组。但是,当我们有WHERE子句时,真的有必要使用它吗 例如: 我有一个名为sales的表,其中包含以下内容: id item_name price month ---------- ---------- ---------- ---------- 1 Bike
id item_name price month
---------- ---------- ---------- ----------
1 Bike 500.0 October
2 Hammer 25.0 October
3 Rope 50.0 November
4 Screw 10.0 November
此查询:
SELECT month,count(*) FROM sales WHERE price > 35 GROUP BY month;
还有这个:
SELECT month,count(*) FROM sales WHERE price > 35 GROUP BY month HAVING (price > 35);
返回相同的结果。我想知道我是否使用HAVING子句的目的正是它的设计目的。因为我运行了另一组查询:
SELECT month,count(*) FROM sales GROUP BY month HAVING (price > 35);
SELECT month,count(*) FROM sales WHERE price > 35 GROUP BY month HAVING (price > 35);
他们都会给出不同的答案
感谢您提供的帮助。如果您再添加一个功能,sumprice: 选择 月 ,伯爵* ,sumprice 来自销售 其中价格>35 按月分组 ; 这将首先使用where子句过滤数据,然后将其分组。由于它在过滤后分组,count*给出1 何处为
select
month
, count(*)
, sum(price)
from sales
group by month
having price > 35
;
在未筛选的数据no where子句上运行,但仅对价格>35的项求和。因为它是在未过滤的数据上运行的,count*给出了2。我检查了答案,阅读了一些博客,得到了答案。谢谢你的帮助。