多表查询问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 07:04:09
a表
aa bb
1 1000
2 1000
3 1002
4 1001

b表
cc dd
1 1000
2 1003
3 1001
4 1001

字段都是char型的
编写sql语句统计a表bb字段中含1000,1001和b表中含1000,1001个数的总和,本例中总和为6
怎么编写,请高人指点

select count(aa) as num
from (select aa as aa ,bb as aa from a
union
select cc as aa,dd as bb from b) as tab
where bb in ('1000','1001')

select
(select count(1) from a where bb in ('1000','1001') ) +
(select count(1) from b where dd in ('1000','1001') )

sqlserver 写法

select count(aa) as num
from (select aa as aa ,bb as aa from a
union
select cc as aa,dd as bb from b) as tab
where bb in ('1000','1001')

没测试,就是个意思