统计mysql多张表总数
场景:统计三张表总数
三种实现方式:
第一种:查询三张表总数
select sum(a) from (select count(*) a from tb1 union select count(*) a from tb2 union select count(*) a from tb3) as tb;
查询结果:
第二种:查询每张表总数
select count(*) 总数 from tb1 union select count(*) b from tb2 union select count(*) c from tb3;
查询结果:
第三种:统计每张表中总数
select a.countNum,b.countNum,c.countNum
from(
(select COUNT(*) as countNum from tb1) a,
(select COUNT(*) as countNum from tb2) b,
(select COUNT(*) as countNum from tb3) c
);