单个表用 select count(*) aags from aa where dq like '上海'; 就能统计出来,但是一次统计4个表,怎么写呢·?
...全文
773打赏收藏
求助多表查询统计的写法
要求如下: 要查询4个表(表名为aa,bb,cc,dd)里面共同字段dq中值为 上海 的记录,并分别统计列出,aa表里面有多少个 上海的记录,bb表里面有多少个上海的记录,cc表里面有多少个上海的记录,dd表里面有多少个上海的记录, 单个表用 select count(*) aags from aa where dq like '上海'; 就能统计出来,但是一次统计4个表,怎么写呢·?
谢谢楼上的两位,
select 'aa' as tname,count(*) aags from aa where dq like '上海'
union all
select 'bb' as tname,count(*) aags from bb where dq like '上海'
union all
select 'cc' as tname,count(*) aags from cc where dq like '上海'
union all
select 'dd' as tname,count(*) aags from dd where dq like '上海'
这样问题解决了;
谢谢·
select 'aa' as tname,count(*) aags from aa where dq like '上海'
union all
select 'bb' as tname,count(*) aags from bb where dq like '上海'
union all
select 'cc' as tname,count(*) aags from cc where dq like '上海'
union all
select 'dd' as tname,count(*) aags from dd where dq like '上海'
select dq,bz,count(*) from (
select 'aa' as bz,* from aa
union all
select 'bb' as bz,* from bb
union all
select 'cc' as bz,* from cc
union all
select 'dd' as bz,* from dd
) a
group by dq,bz