SQL 从结果查询中选择 count(*)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13837704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select count(*) from result query
提问by Butters
I need help from you, this is my sql query:
我需要你的帮助,这是我的 sql 查询:
select count(SID)
from Test
where Date = '2012-12-10'
group by SID
this is my result:
这是我的结果:
|2|
|3|
|4|
|3|
and now I have to count the results from first query!
现在我必须计算第一次查询的结果!
Expected result: 4
回答by Taryn
You can wrap your query in another SELECT
:
您可以将查询包装在另一个中SELECT
:
select count(*)
from
(
select count(SID) tot -- add alias
from Test
where Date = '2012-12-10'
group by SID
) src; -- add alias
In order for it to work, the count(SID)
need a column alias and you have to provide an alias to the subquery itself.
为了使其工作,count(SID)
需要一个列别名,并且您必须为子查询本身提供一个别名。
回答by dan1111
This counts the rows of the inner query:
这会计算内部查询的行数:
select count(*) from (
select count(SID)
from Test
where Date = '2012-12-10'
group by SID
) t
However, in this case the effect of that is the same as this:
但是,在这种情况下,其效果与此相同:
select count(distinct SID) from Test where Date = '2012-12-10'
回答by Daniel Mács
select count(*) from(select count(SID) from Test where Date = '2012-12-10' group by SID)
select count(*) from(select count(SID) from Test where Date = '2012-12-10' group by SID)
select count(*) from(select count(SID) from Test where Date = '2012-12-10' group by SID)
select count(*) from(select count(SID) from Test where Date = '2012-12-10' group by SID)
should works
应该有效