Select Count(1) as COUNT1 from
(
Select A,B from table1
union
Select A, B from table2
)
Select Count(1) as COUNT2 from
(
Select C,D from table3
union
Select C,D from table4
)
Select Count(1) as COUNT3 from
(
Select E,F from table5
union
Select E,F from table6
);
我想显示一个显示COUNT1,COUNT2,COUNT3的表格。
答案 0 :(得分:1)
SELECT a.COUNT1 ,
b.COUNT2,
c.COUNT3
FROM
(SELECT COUNT(1) AS COUNT1
FROM
( SELECT A,B FROM table1
UNION
SELECT A, B FROM table2
)
) AS a,
(SELECT COUNT(1) AS COUNT2
FROM
( SELECT C,D FROM table3
UNION
SELECT C,D FROM table4
)
) AS b,
(SELECT COUNT(1) AS COUNT3
FROM
( SELECT E,F FROM table5
UNION
SELECT E,F FROM table6
)
) AS c;
答案 1 :(得分:1)
试试这个:
SELECT SUM(COUNT1) AS COUNT1, SUM(COUNT2) AS COUNT2, SUM(COUNT3) AS COUNT3
FROM
(
Select Count(1) as COUNT1, 0 AS COUNT2, 0 AS COUNT3 from
(
Select A,B from table1
union
Select A, B from table2
)
UNION ALL
Select 0 AS COUNT1, Count(1) as COUNT2, 0 AS COUNT3 from
(
Select C,D from table3
union
Select C,D from table4
)
UNION ALL
Select 0 AS COUNT1, 0 AS COUNT2, Count(1) as COUNT3 from
(
Select E,F from table5
union
Select E,F from table6
)
) AS a
基本上,您为每个源表添加1行,同时为不相关的计数添加0值。最后,对结果集执行SUM,最后得到一个包含正确计数的行。