日期:2014-05-18  浏览次数:20579 次

求一个简单的SQL语句 请大家帮忙
正在编程中 遇到一点小问题 请大家帮帮忙
SQL SERVER 2000
2个表

表名:order 
idd spgg sl 
1 608 200
2 608 200



表名:orderMX
reidd spgg jhsl 
1 608 100


返回结果:
idd spgg sl jhsl
1 608 200 100
2 608 200 0




------解决方案--------------------
SQL code
select a.*,isnull(b.jhsl,0)
from [order] a left join orderMX b on a.idd =b.reidd

------解决方案--------------------
SQL code

select idd,spgg,sl,isnull(jhsl,0)jhsl from order t1,orderMX t2 where t1.idd=t2.idd

------解决方案--------------------
SQL code
select a.idd,a.spgg,a.sl,case when b.jhsl is null then 0 else b.jhsl end jhsl 
from order a left join orderMX b on a.idd=b.reidd

------解决方案--------------------
SQL code
SQL code
select t1.idd,t1.spgg,sl,isnull(jhsl,0)jhsl from order t1,orderMX t2 where t1.idd=t2.idd

------解决方案--------------------
SQL code

--测试数据
declare @order table (idd int,spgg int,sl int)
insert into @order
select 1,608,200 union all
select 2,608,200
--测试数据
declare @orderMX table (reidd int,spgg int,jhsl int)
insert into @orderMX
select 1,608,100 

select a.*,isnull(b.jhsl,0) as jhsl 
from @order a left join @orderMX b on a.idd=b.reidd

/*
idd         spgg        sl          jhsl
----------- ----------- ----------- -----------
1           608         200         100
2           608         200         0
*/