两种使用 Count 的方法,它们等效吗?
相当于
SELECT COUNT(a.attr)
FROM TABLE a
我
SELECT B
FROM (SELECT COUNT(a.attr) as B
FROM TABLE a)
猜不会,但我不确定。
我还假设对于 min、max、avg 等函数,答案是相同的,对吗?
编辑:
这完全是出于好奇,我对此还是新手。下面的计数和上面的计数返回的值有区别吗?
SELECT B, C
FROM (SELECT COUNT(a.attr) as B, a.C
FROM TABLE a
GROUP BY c)
再次编辑:我调查了它,吸取了教训:当我尝试了解这些事情时,我应该保持清醒。
Is
SELECT COUNT(a.attr)
FROM TABLE a
equivalent to
SELECT B
FROM (SELECT COUNT(a.attr) as B
FROM TABLE a)
I would guess no, but I'm not sure.
I'm also assuming the answer would be the same for functions like min, max, avg, correct?
EDIT:
This is all out of curiosity, I'm still new at this. Is there a difference between the value returned for the count of the following and the above?
SELECT B, C
FROM (SELECT COUNT(a.attr) as B, a.C
FROM TABLE a
GROUP BY c)
EDIT AGAIN: I looked into it, lesson learned: I should be awake when I try to learn about these things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从技术上讲,它们是不一样的,第一个是简单选择,第二个是带有子选择的选择。
但每个理智的优化器都会为它们生成相同的执行计划。
Technically, they are not the same, the first one is a simple select, the second one is a select with a sub select.
But every sane optimizer will generate the same execution plan for both of them.
结果是相同的,并且将相同于:
并且同样毫无意义。
第二个查询本质上是混淆
COUNT
,应该避免。编辑:
是的,添加到 OP 中的编辑查询是同一件事。它只是无缘无故地添加了一个子查询。
The results are the same, and would be the same as:
And equally as pointless.
The second query is essentially obfuscating a
COUNT
and should be avoided.EDIT:
Yes, your edited query that was added to the OP is the same thing. It's just adding a subquery for no reason.
我发布这个答案是为了补充其他答案中已经说过的内容,并且因为您无法格式化注释:)
您可以随时检查执行计划以查看查询是否等效;这就是 SQL Server 的原理:
这是
SELECT
语句的执行计划,您可以看到前两个语句没有区别:Am posting this answer to supplement what has already been said in the other answers, and because you cannot format comments :)
You can always check the execution plan to see if queries are equivalent; this is what SQL Server makes of it:
Here's the execution plan of the
SELECT
statments, as you can see there is no difference in the first two:是的,有。您在第二个中所做的所有操作就是将返回的计数命名为 B。它们将返回相同的结果。
http://www.roseindia.net/sql/sql-as-keyword.shtml
编辑:
更好的例子:
http://www.w3schools.com/sql/sql_alias.asp
第三个示例将有所不同,因为它包含一个分组依据。它将返回每个不同 aC 条目的计数。示例
将返回
不一定按该顺序
检查所有这些的最简单方法是亲自尝试并查看它返回什么。
Yes there are. All your doing in the second one is naming the returned count B. They will return the same results.
http://www.roseindia.net/sql/sql-as-keyword.shtml
EDIT:
Better example:
http://www.w3schools.com/sql/sql_alias.asp
The third example will be different because it contains a group by. It will return the count for every distinct a.C entry. Example
Would return
Not necessarily in that order
Easiest way to check all of this is to try it for yourself and see what it returns.
您的第一个代码示例是正确的,但第二个代码示例没有任何意义。
您只需选择所有数据两次,无需任何操作。
因此,第一个和第二个样本的输出将相等。
Your first code sample is correct, but second does not have any sense.
You just select all data twice without any operations.
So, output for first and second samples will be equal.