create table tb2 (a int,b int ,c int)
insert into tb1 select 1 union select 2 union select 3 union select 4 union select 5
insert tb2
select 1 , 2 , 3 union all
select 1 , 2 , 3 union all
select 1 , 2 , 3 union all
select 2 , 3 , 1 union all
select 2 , 3 , 1 union all
select 2 , 3 , 1
Select * from (
select a.aaa,
sum(case when a.aaa=b.a then 1 else 0 end) as a中出现的次数,
sum(case when a.aaa=b.b then 1 else 0 end) as b中出现的次数,
sum(case when a.aaa=b.c then 1 else 0 end) as c中出现的次数
from tb1 a,tb2 b group by a.aaa
) A
Where a中出现的次数<>0 Or b中出现的次数<>0 Or c中出现的次数<>0
create table tb2 (a int,b int ,c int)
insert into tb1 select 1 union select 2 union select 3
insert tb2
select 1 , 2 , 3 union all
select 1 , 2 , 3 union all
select 1 , 2 , 3 union all
select 2 , 3 , 1 union all
select 2 , 3 , 1 union all
select 2 , 3 , 1
select a.aaa,
sum(case when a.aaa=b.a then 1 else 0 end) as a中出现的次数,
sum(case when a.aaa=b.b then 1 else 0 end) as b中出现的次数,
sum(case when a.aaa=b.c then 1 else 0 end) as c中出现的次数
from tb1 a,tb2 b group by a.aaa
select a.aaa,sum(case when a.aaa=b.a then 1 else 0 end) as a中出现的次数,sum(case when a.aaa=b.b then 1 else 0 end) as b中出现的次数,sum(case when a.aaa=b.c then 1 else 0 end) as c中出现的次数 from @tb1 a,@tb2 b group by a.aaa