日期:2014-05-17 浏览次数:20556 次
SELECT A.MA003 AS MB005, B.MA003 AS MB006, C.MA003 AS MB007, D.MA003 AS MB008, PN FROM 表A LEFT OUTER JOIN 表B AS A ON 表A.MB005 = A.MA003 LEFT OUTER JOIN 表B AS B ON 表A.MB005 = B.MA003 LEFT OUTER JOIN 表B AS C ON 表A.MB005 = C.MA003 LEFT OUTER JOIN 表B AS D ON 表A.MB005 = D.MA003
------解决方案--------------------
create table #表A(MB005 varchar(4),MB006 varchar(4),MB007 varchar(4),MB008 varchar(4),PN varchar(4)) insert into #表A select '113', 'null', 'null' ,'406','AA' union all select '113', '311' , 'null' ,'406','BB' union all select '112', 'null', '2190' ,'406','CC' create table #表B(MB002 varchar(4),MB003 varchar(10)) insert into #表B select '113','实验品' union all select '112','原材料' union all select '311', '前处理' union all select '2190','ROHS类' union all select '406','大宗物品' select B.MB003 MB003,C.MB003 MB006,D.MB003 MB007,E.MB003 MB008,A.PN PN from #表A A left join #表B B on A.MB005=B.MB002 left join #表B C on A.MB006=C.MB002 left join #表B D on A.MB007=D.MB002 left join #表B E on A.MB008=E.MB002 --结果 实验品 NULL NULL 大宗物品 AA 实验品 前处理 NULL 大宗物品 BB 原材料 NULL ROHS类 大宗物品 CC --消息 (3 行受影响)
------解决方案--------------------
--道兄正解 SELECT a.ma003 AS mb005, b.ma003 AS mb006, c.ma003 AS mb007, d.ma003 AS mb008, pn FROM 表a LEFT OUTER JOIN 表b AS a ON 表a.mb005 = a.ma003 LEFT OUTER JOIN 表b AS b ON 表a.mb005 = b.ma003 LEFT OUTER JOIN 表b AS c ON 表a.mb005 = c.ma003 LEFT OUTER JOIN 表b AS d ON 表a.mb005 = d.ma003