是否有子句“无用”在选择语句中?

发布于 2025-01-31 13:47:46 字数 777 浏览 6 评论 0原文

我正在阅读一篇名为查询优化技术 - 写作技巧 高效,更快的SQL查询

该文档表明在选择语句中,hastar子句是“无用的”:

提示#2:避免在Select语句中包含一个子句

所有行是在所有行之后用于过滤行的 选择,它像过滤器一样使用。 在选择中是没有用的 语句。它可以通过浏览最终结果表 查询解析不符合条件的行。

示例:

原始查询:

 选择s.cust_id,count(s.cust_id)
来自Sh.sales s
s.cust_id组
具有s.cust_id!='1660'和s.cust_id!='2';
 

改进的查询:

 选择s.cust_id,count(cust_id)
来自Sh.sales s
其中s.cust_id!='1660'
和s.cust_id!='2'
s.cust_id组;
 

问题:

该断言正确吗?在某些语句中,HASTER子句没有目的吗?

I’m reading a paper called Query Optimization Techniques - Tips For Writing
Efficient And Faster SQL Queries
.

That document suggests that the HAVING clause is “useless” in a SELECT statement:

Tip #2: Avoid including a HAVING clause in SELECT statements

The HAVING clause is used to filter the rows after all the rows are
selected and it is used like a filter. It is quite useless in a SELECT
statement.
It works by going through the final result table of the
query parsing out the rows that don’t meet the HAVING condition.

Example:

Original query:

SELECT s.cust_id,count(s.cust_id)
FROM SH.sales s
GROUP BY s.cust_id
HAVING s.cust_id != '1660' AND s.cust_id != '2';

Improved query:

SELECT s.cust_id,count(cust_id)
FROM SH.sales s
WHERE s.cust_id != '1660'
AND s.cust_id !='2'
GROUP BY s.cust_id;

Question:

Is that assertion correct? Does the HAVING clause not have a purpose in a SELECT statement?

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

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

发布评论

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

评论(1

剧终人散尽 2025-02-07 13:47:46

如果它没有用,那将不存在。

使用have子句将返回行的组限制为指定条件为true

的那些组

根据示例为true的那些群体,您将其用作

SELECT s.cust_id,count(cust_id)
FROM SH.sales s
WHERE s.cust_id != '1660'
AND s.cust_id !='2'
GROUP BY s.cust_id
HAVING count(cust_id) > 5;       --> here

其目的是什么?您不能使用以及汇总功能的,例如,这是无效的:

FROM ...
WHERE count(cust_id) > 5    --> this
AND ...

If it were useless, it wouldn't exist.

Use the HAVING clause to restrict the groups of returned rows to those groups for which the specified condition is TRUE

Based on your example, you'd use it as e.g.

SELECT s.cust_id,count(cust_id)
FROM SH.sales s
WHERE s.cust_id != '1660'
AND s.cust_id !='2'
GROUP BY s.cust_id
HAVING count(cust_id) > 5;       --> here

What is its purpose? You can't use WHERE along with aggregate functions, e.g. this is invalid:

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