select isnull(a.[id],b.[id]) as [id],
isnull(a.modelcost,0) as modelcost,
isnull(a.modcompany,0) as modcompany,
isnull(b.stonecost,0) as stonecost,
isnull(b.stocompany,0) as stocompany
from @s1 a
full join @s2 b
on a.[id]=b.[id]
order by [id]
select
id = (case when a.id is null then b.id else a.id end),
modelcost = isnull(a.modelcost ,0),
modcompany = isnull(a.modcompany,0),
stonecost = isnull(b.stonecost ,0),
stocompany = isnull(b.stocompany,0)
from
test1 a
full outer join
test2 b
on
a.id = b.id
看了樓上幾位代碼,我覺得都沒有精確理解樓主的意思,代碼都是錯誤的,應該如下寫才正確:
select ISNULL(t1.id,t2.id) id,isnull(t1.modelcost,0) modelcost,isnull(t1.modcompany,0) modcompany,isnull(t2.stonecost,0) stonecost,isnull(t2.stocompany,0) stocompany into test3 from test1 t1 FULL JOIN test2 t2 on t1.id=t2.id
--上面test1和a中间没加空格
select isnull(a.id,b.id)as id,isnull(modelcost,0)as modelcost,isnull(modcompany,0)as modcompany,isnull(stonecost,0)as stonecost,isnull(stocompany,0)as stocompany into test3 from test1 a left join test2 b on a.id=b.id
select isnull(a.id,b.id)as id,isnull(modelcost,0)as modelcost,isnull(modcompany,0)as modcompany,isnull(stonecost,0)as stonecost,isnull(stocompany,0)as stocompany into test3 from test1a left join test2 b on a.id=b.id
select ISNULL(t1.id,t2.id) as id,
isnull(t1.modelcost,0) as modelcost,
isnull(t1.modcompany,0) as modcompany,
isnull(t2.stonecost,0) as stonecost,
isnull(t2.stocompany,0) as stocompany into test3
from test1 t1 FULL JOIN test2 t2 on t1.id=t2.id
楼主试试吧,应该没问题啦!!!!!!
select isnull(a.[id],b.[id]) as [id],
isnull(a.modelcost,0) as modelcost,
isnull(a.modcompany,0) as modcompany,
isnull(b.stonecost,0) as stonecost,
isnull(b.stocompany,0) as stocompany
from table1 a
full join table2 b
on a.[id]=b.[id]
order by [id]
select isnull(a.[id],b.[id]) as [id],
isnull(a.modelcost,0) as modelcost,
isnull(a.modcompany,0) as modcompany,
isnull(b.stonecost,0) as stonecost,
isnull(b.stocompany,0) as stocompany
from table1 a
full join table2 b
on a.[id]=b.[id]
order by [id]