单个查询以查找SQL中多个表的行数

时间:2014-03-09 11:14:10

标签: sql

我有一个名为Test的模式,它有表A, B, C

我想在一个命令中获取每个表的count(row)

2 个答案:

答案 0 :(得分:1)

select count(*) as a_count, null as b_count, null as c_count from A
union
select null, count(*), null from B
union
select null, null, count(*) from C

答案 1 :(得分:0)

这也有效:

SELECT (SELECT COUNT(*) 
          FROM A       ) AS A_count, 
       (SELECT COUNT(*) 
          FROM B       ) AS B_count, 
       (SELECT COUNT(*) 
          FROM C       ) AS C_count;
  • 通过这种方式,您可以获得一行的结果(而不是三行 UNION
  • 代码减少,因为您不需要所有NULL