SQL:从多个表中组合选择计数(*)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1279569/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:12:50  来源:igfitidea点击:

SQL: Combine Select count(*) from multiple tables

sqlsql-servertsql

提问by Hyman

How do you combine multiple select count(*) from different table into one return?

如何将不同表中的多个 select count(*) 合并为一个 return?

I have a similar sitiuation as this post

我和这个帖子有类似的情况

but I want one return.

但我想要一个回报。

I tried Union all but it spit back 3 separate rows of count. How do you combine them into one?

我尝试了 Union all,但它吐出 3 个单独的计数行。你如何将它们合二为一?

select count(*) from foo1 where ID = '00123244552000258'
union all 
select count(*) from foo2 where ID = '00123244552000258'
union all
select count(*) from foo3 where ID = '00123244552000258'

edit: I'm on MS SQL 2005

编辑:我在 MS SQL 2005

回答by Chris J

SELECT 
(select count(*) from foo1 where ID = '00123244552000258')
+
(select count(*) from foo2 where ID = '00123244552000258')
+
(select count(*) from foo3 where ID = '00123244552000258')

This is an easy way.

这是一个简单的方法。

回答by Remus Rusanu

select 
  (select count(*) from foo) as foo
, (select count(*) from bar) as bar
, ...

回答by Bill Karwin

I'm surprised no one has suggested this variation:

我很惊讶没有人提出这种变化:

SELECT SUM(c)
FROM (
  SELECT COUNT(*) AS c FROM foo1 WHERE ID = '00123244552000258'
  UNION ALL
  SELECT COUNT(*) FROM foo2 WHERE ID = '00123244552000258'
  UNION ALL
  SELECT COUNT(*) FROM foo3 WHERE ID = '00123244552000258'
);

回答by Mitchel Sellers

Basically you do the counts as sub-queries within a standard select.

基本上,您将计数作为标准选择中的子查询。

An example would be the following, this returns 1 row, two columns

一个例子如下,这将返回 1 行,两列

SELECT
 (SELECT COUNT(*) FROM MyTable WHERE MyCol = 'MyValue') AS MyTableCount,
 (SELECT COUNT(*) FROM YourTable WHERE MyCol = 'MyValue') AS YourTableCount,

回答by Jason

You can combine your counts like you were doing before, but then you could sum them all up a number of ways, one of which is shown below:

您可以像以前一样组合您的计数,但随后您可以通过多种方式将它们汇总起来,其中一种如下所示:

SELECT SUM(A) 
FROM
(
    SELECT 1 AS A
    UNION ALL 
    SELECT 1 AS A
    UNION ALL
    SELECT 1 AS A
    UNION ALL
    SELECT 1 AS A
) AS B

回答by Gren

select sum(counts) from (
select count(1) as counts from foo 
union all
select count(1) as counts from bar)

回答by toquart

For oracle:

对于甲骨文:

select( 
select count(*) from foo1 where ID = '00123244552000258'
+
select count(*) from foo2 where ID = '00123244552000258'
+
select count(*) from foo3 where ID = '00123244552000258'
) total from dual;

回答by Kris

you could name all fields and add an outer select on those fields:

您可以命名所有字段并在这些字段上添加外部选择:

SELECT A, B, C FROM ( your initial query here ) TableAlias

That should do the trick.

这应该够了吧。