HAVING 子句后面可以有 WHERE 子句吗?

发布于 2024-08-10 03:16:39 字数 92 浏览 7 评论 0原文

是否可以在 HAVING 子句后使用 WHERE 子句?

我首先想到的是子查询,但我不确定。

PS 如果答案是肯定的,你能举一些例子吗?

Is it possible to use a WHERE clause after a HAVING clause?

The first thing that comes to my mind is sub queries, but I'm not sure.

P.S. If the answer is affirmative, could you give some examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月无声 2024-08-17 03:16:39

不,不在同一个查询中。

where 子句位于 havinggroup by 之前。如果您想在分组之前过滤掉记录,则条件位于 where 子句中,如果您想过滤掉分组记录,则条件位于 having 子句中

select ...
from ...
where ...
group by ...
having ...

:由于某些奇怪的原因,这两个查询都无法使用,您必须将查询设置为子查询,以便可以将 where 子句放在外部查询中:

select ...
from (
   select ...
   from ...
   where ...
   group by ...
   having ...
) x
where ...

No, not in the same query.

The where clause goes before the having and the group by. If you want to filter out records before the grouping the condition goes in the where clause, and if you want to filter out grouped records the condition goes in the having clause:

select ...
from ...
where ...
group by ...
having ...

If neither of those are possible to use for some odd reason, you have to make the query a subquery so that you can put the where clause in the outer query:

select ...
from (
   select ...
   from ...
   where ...
   group by ...
   having ...
) x
where ...
GRAY°灰色天空 2024-08-17 03:16:39

HAVING 子句只是 GROUP BY 之后的 WHERE 子句。为什么不将 WHERE 条件放在 HAVING 子句中?

A HAVING clause is just a WHERE clause after a GROUP BY. Why not put your WHERE conditions in the HAVING clause?

坠似风落 2024-08-17 03:16:39

如果这是一个棘手的问题,那么如果 WHERE 和 HAVING 不在同一级别,正如您提到的,使用子查询是可能的。

我想这样的事情会起作用

有值=(选择最大值(值)FROM
foo WHERE crit=123)

ps: 你为什么问?
您有具体问题吗?

pss:好吧,愚蠢的我,我错过了“采访*”标签......

If it's a trick question, it's possible if the WHERE and the HAVING are not at the same level, as you mentionned, with subquery.

I guess something like that would work

HAVING value=(SELECT max(value) FROM
foo WHERE crit=123)

p.s.: why were you asking?
Do you have a specific problem?

p.s.s: OK silly me, I missed the "interview*" tag...

蛮可爱 2024-08-17 03:16:39

来自 SELECT 帮助

WHERE、GROUP BY、的处理顺序
和 HAVING 子句以下步骤
显示 SELECT 的处理顺序
带有 WHERE 子句的语句、GROUP
BY 子句和 HAVING 子句:

FROM 子句返回初始值
结果集。

WHERE 子句不排除行
满足其搜索条件。

GROUP BY 子句收集
将选定的行分为一组
GROUP BY 子句中的唯一值。

中指定的聚合函数
选择列表计算汇总值
每个组。

另外还有 HAVING 子句
排除不符合搜索条件的行
情况。

所以,不,你不能。

From SELECT help

Processing Order of WHERE, GROUP BY,
and HAVING Clauses The following steps
show the processing order for a SELECT
statement with a WHERE clause, a GROUP
BY clause, and a HAVING clause:

The FROM clause returns an initial
result set.

The WHERE clause excludes rows not
meeting its search condition.

The GROUP BY clause collects the
selected rows into one group for each
unique value in the GROUP BY clause.

Aggregate functions specified in the
select list calculate summary values
for each group.

The HAVING clause additionally
excludes rows not meeting its search
condition.

So, no you can not.

你的往事 2024-08-17 03:16:39

在同样的范围内,答案是否定的。如果允许子查询,那么您可以完全避免使用HAVING

我认为 HAVING 是不合时宜的。 Hugh Darwen 将 HAVING 称为“结构化查询的愚蠢行为”:

在旧的 SQL 中,WHERE 子句不能
用于聚合结果,所以
他们必须发明HAVING(具有相同的
含义为 WHERE):

SELECT D#, AVG(工资) AS Avg_Sal
  来自雇员
 团体 
    由D#
平均工资(工资)> 999;

但是如果我们有HAVING
1979 年,人们可以这样写:

<前><代码>选择*
从 (
选择 D#,AVG(Sal) 作为 Avg_Sal
来自员工
团体
由D#

AS假人
其中 Avg_Sal > 999;

我强烈怀疑达文问题的答案是否定的。

Within the same scope, answer is no. If subqueries is allowed then you can avoid using HAVING entirely.

I think HAVING is an anachronism. Hugh Darwen refers to HAVING as "The Folly of Structured Queries":

In old SQL, the WHERE clause could not
be used on results of aggregation, so
they had to invent HAVING (with same
meaning as WHERE):

SELECT D#, AVG(Salary) AS Avg_Sal
  FROM Emp
 GROUP 
    BY D#
HAVING AVG(Salary) > 999;

But would we ever have had HAVING if
in 1979 one could write:

SELECT * 
  FROM (
        SELECT D#, AVG(Sal) AS Avg_Sal
          FROM Emp
         GROUP 
            BY D# 
       )
      AS dummy
WHERE Avg_Sal > 999;

I strongly suspect the answer to Darwen's question is no.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文