日期:2014-05-17  浏览次数:20547 次

SQL合并查询
有2个表Ta,Tb

Ta:
ID Acout
111  1
22   18
332   9

Tb:
ID  Bcout
111   2
3    34

其中 两表的ID值有相同值
我想得到的结果是,把两表的ID放在一列,如下:
ID Acout Bcout
111  1     2
22   18    null
332  9     null
3    null  34

这样。。


------解决方案--------------------
select isnull(a.id,b.id) as id,a.Acout,b.Bcout
from a
full join b on a.id=b.id 
------解决方案--------------------
Ta
ID,Postil


Tb
ID,b_Postil


语句:

select ID,sum(case when postil is null then 1 else 0 end) Postil
from(
    select ID,Postil from ta
    union all
    select ID,b_Postil from tb
) tmp
group by ID