HAVING 子句后面可以有 WHERE 子句吗?
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,不在同一个查询中。
where
子句位于having
和group by
之前。如果您想在分组之前过滤掉记录,则条件位于where
子句中,如果您想过滤掉分组记录,则条件位于having
子句中:由于某些奇怪的原因,这两个查询都无法使用,您必须将查询设置为子查询,以便可以将
where
子句放在外部查询中:No, not in the same query.
The
where
clause goes before thehaving
and thegroup by
. If you want to filter out records before the grouping the condition goes in thewhere
clause, and if you want to filter out grouped records the condition goes in thehaving
clause: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: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?
如果这是一个棘手的问题,那么如果 WHERE 和 HAVING 不在同一级别,正如您提到的,使用子查询是可能的。
我想这样的事情会起作用
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
p.s.: why were you asking?
Do you have a specific problem?
p.s.s: OK silly me, I missed the "interview*" tag...
来自 SELECT 帮助
所以,不,你不能。
From SELECT help
So, no you can not.
在同样的范围内,答案是否定的。如果允许子查询,那么您可以完全避免使用
HAVING
。我认为
HAVING
是不合时宜的。 Hugh Darwen 将HAVING
称为“结构化查询的愚蠢行为”:我强烈怀疑达文问题的答案是否定的。
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 toHAVING
as "The Folly of Structured Queries":I strongly suspect the answer to Darwen's question is no.