SQL - HAVING 与 WHERE
我有以下两个表:
1. Lecturers (LectID, Fname, Lname, degree).
2. Lecturers_Specialization (LectID, Expertise).
我想找到最专业化的讲师。 当我尝试这个时,它不起作用:
SELECT
L.LectID,
Fname,
Lname
FROM Lecturers L,
Lecturers_Specialization S
WHERE L.LectID = S.LectID
AND COUNT(S.Expertise) >= ALL (SELECT
COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);
但是当我尝试这个时,它起作用:
SELECT
L.LectID,
Fname,
Lname
FROM Lecturers L,
Lecturers_Specialization S
WHERE L.LectID = S.LectID
GROUP BY L.LectID,
Fname,
Lname
HAVING COUNT(S.Expertise) >= ALL (SELECT
COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);
原因是什么?谢谢。
I have the following two tables:
1. Lecturers (LectID, Fname, Lname, degree).
2. Lecturers_Specialization (LectID, Expertise).
I want to find the lecturer with the most Specialization.
When I try this, it is not working:
SELECT
L.LectID,
Fname,
Lname
FROM Lecturers L,
Lecturers_Specialization S
WHERE L.LectID = S.LectID
AND COUNT(S.Expertise) >= ALL (SELECT
COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);
But when I try this, it works:
SELECT
L.LectID,
Fname,
Lname
FROM Lecturers L,
Lecturers_Specialization S
WHERE L.LectID = S.LectID
GROUP BY L.LectID,
Fname,
Lname
HAVING COUNT(S.Expertise) >= ALL (SELECT
COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);
What is the reason? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
WHERE
子句引入了各个行的条件;HAVING
子句引入了聚合的条件,即选择结果,其中单个结果(例如计数、平均值、最小值、最大值或总和)已从生成>多行。您的查询需要第二种条件(即聚合条件),因此HAVING
可以正常工作。根据经验,请在
GROUP BY
之前使用WHERE
,在GROUP BY
之后使用HAVING
。这是一个相当原始的规则,但在 90% 以上的情况下都是有用的。当您执行此操作时,您可能需要使用 ANSI 版本的连接重新编写查询:
这将消除用作 theta 连接条件的
WHERE
。WHERE
clause introduces a condition on individual rows;HAVING
clause introduces a condition on aggregations, i.e. results of selection where a single result, such as count, average, min, max, or sum, has been produced from multiple rows. Your query calls for a second kind of condition (i.e. a condition on an aggregation) henceHAVING
works correctly.As a rule of thumb, use
WHERE
beforeGROUP BY
andHAVING
afterGROUP BY
. It is a rather primitive rule, but it is useful in more than 90% of the cases.While you're at it, you may want to re-write your query using ANSI version of the join:
This would eliminate
WHERE
that was used as a theta join condition.首先我们应该知道条款的执行顺序,即
来自>哪里>分组依据>具有>独特>选择>订购依据。
由于 WHERE 子句在 GROUP BY 子句之前执行,因此无法通过将 WHERE 应用于 GROUP BY 来过滤记录记录。
“HAVING 与 WHERE 子句相同,但应用于分组记录”。
首先,WHERE 子句根据条件获取记录,然后 GROUP BY strong>子句相应地对它们进行分组,然后HAVING子句根据having条件获取组记录。
First we should know the order of execution of Clauses i.e
FROM > WHERE > GROUP BY > HAVING > DISTINCT > SELECT > ORDER BY.
Since WHERE Clause gets executed before GROUP BY Clause the records cannot be filtered by applying WHERE to a GROUP BY applied records.
"HAVING is same as the WHERE clause but is applied on grouped records".
first the WHERE clause fetches the records based on the condition then the GROUP BY clause groups them accordingly and then the HAVING clause fetches the group records based on the having condition.
HAVING
对聚合进行操作。由于COUNT
是聚合函数,因此您不能在WHERE
子句中使用它。这里来自 MSDN 的一些汇总内容功能。
HAVING
operates on aggregates. SinceCOUNT
is an aggregate function, you can't use it in aWHERE
clause.Here's some reading from MSDN on aggregate functions.
WHERE
子句可与SELECT
、INSERT
和UPDATE
语句一起使用,而HAVING
只能与SELECT
语句一起使用。WHERE
在聚合之前过滤行 (GROUP BY
),而HAVING
在执行聚合之后过滤组。聚合函数不能在
WHERE
子句中使用,除非它位于HAVING
子句中包含的子查询中,而聚合函数可以在HAVING
中使用> 条款。来源
WHERE
clause can be used withSELECT
,INSERT
, andUPDATE
statements, whereasHAVING
can be used only withSELECT
statement.WHERE
filters rows before aggregation (GROUP BY
), whereasHAVING
filter groups after aggregations are performed.Aggregate function cannot be used in
WHERE
clause unless it is in a subquery contained inHAVING
clause, whereas aggregate functions can be used inHAVING
clause.Source
在一个查询中没有看到两者的示例。所以这个例子可能会有所帮助。
这首先按 companyId 过滤表,然后将其分组(按国家和城市),最后将其过滤到仅墨西哥的城市聚合。聚合中不需要 companyId,但我们可以在使用 GROUP BY 之前使用 WHERE 过滤出我们想要的行。
Didn't see an example of both in one query. So this example might help.
This filters the table first by the companyId, then groups it (by country and city) and additionally filters it down to just city aggregations of Mexico. The companyId was not needed in the aggregation but we were able to use WHERE to filter out just the rows we wanted before using GROUP BY.
您不能将 where 子句与聚合函数一起使用,因为 where 根据条件获取记录,它会逐条记录地进入表记录,然后根据我们给出的条件获取记录。所以到时候我们就可以不用where子句了。而having子句适用于我们在运行查询后最终得到的结果集。
示例查询:
这会将结果集存储在临时内存中,然后having 子句将执行其工作。所以我们在这里可以很容易地使用聚合函数。
You can not use where clause with aggregate functions because where fetch records on the basis of condition, it goes into table record by record and then fetch record on the basis of condition we have give. So that time we can not where clause. While having clause works on the resultSet which we finally get after running a query.
Example query:
This will store the resultSet in a temporary memory, then having clause will perform its work. So we can easily use aggregate functions here.
1.
我们可以将聚合函数与 HAVING 子句一起使用,而不是与 WHERE 子句一起使用,例如 min、max、avg。
2.
WHERE 子句逐元组消除记录元组
HAVING 子句从组的集合中消除整个组。
当您有数据组时,通常使用 HAVING,而当您有行数据时,则使用 WHERE。
1.
We can use aggregate function with HAVING clause not by WHERE clause e.g. min,max,avg.
2.
WHERE clause eliminates the record tuple by tuple
HAVING clause eliminates entire group from the collection of group
Mostly HAVING is used when you have groups of data and WHERE is used when you have data in rows.
WHERE
子句用于消除关系中的元组,HAVING
子句用于消除关系中的组。HAVING
子句用于聚合函数,例如MIN
,MAX
,COUNT
,SUM
。但始终在之前使用GROUP BY
子句HAVING
子句可最大限度地减少错误。WHERE
clause is used to eliminate the tuples in a relation,andHAVING
clause is used to eliminate the groups in a relation.HAVING
clause is used for aggregate functions such asMIN
,MAX
,COUNT
,SUM
.But always useGROUP BY
clause beforeHAVING
clause to minimize the error.WHERE
和HAVING
都用于过滤数据。对于
WHERE
语句,数据过滤发生在您提取数据进行操作之前。这里,
WHERE
子句在执行SELECT
操作之前过滤行。HAVING
在执行SELECT
操作后过滤数据。这里首先完成计算(聚合)操作,然后使用HAVING
子句对结果应用过滤器。Both
WHERE
andHAVING
are used to filter data.In case of a
WHERE
statement, data filtering happens before you pull the data for operation.Here the
WHERE
clause filters rows before theSELECT
operation is performed.HAVING
filters the data after theSELECT
operation is performed. Here the operation of computing (aggregation) is done first and then a filter is applied to the result using aHAVING
clause.