日期:2014-05-18 浏览次数:20748 次
zb col1 col2 col3 je1 je2 je3 a1 b1 c1 10 10 10 a2 b2 c2 5 4 4 a3 b3 c3 10 10 10 .... jh col1 col2 col3 jhje a1 b1 c1 2 a2 b2 c2 3 .... 需求: tab col1 col2 col3 je1 je2 je3 jhje a1 b1 c1 10 10 10 2 a2 b2 c2 5 4 4 3 a3 b3 c3 10 10 10 0 .... zb表的数据多余jh表,我用left join 来连接,老是数据有问题。
select a.*,isnull(b.jhje,0) as jhje from zb a left join jh b on a.col1 = b.col1 and a.col2 = b.col2 and a.col3 = b.col3
------解决方案--------------------
create table zb(col1 varchar(10),col2 varchar(10),col3 varchar(10),je1 int,je2 int,je3 int) insert into zb select 'a1','b1','c1',10,10,10 insert into zb select 'a2','b2','c2',5, 4, 4 insert into zb select 'a3','b3','c3',10,10, 10 create table jh(col1 varchar(10),col2 varchar(10),col3 varchar(10),jhje int) insert into jh select 'a1','b1','c1',2 insert into jh select 'a2','b2','c2',3 go select a.*,isnull(b.jhje,0)jhje from zb a left join jh b on a.col1=b.col1 and a.col2=b.col2 and a.col3=b.col3 /* col1 col2 col3 je1 je2 je3 jhje ---------- ---------- ---------- ----------- ----------- ----------- ----------- a1 b1 c1 10 10 10 2 a2 b2 c2 5 4 4 3 a3 b3 c3 10 10 10 0 (3 行受影响) */ go drop table zb,jh
------解决方案--------------------
select t1.col1, t1.col2, t1.col3, t1.je1, t1.je2, isnull(t2.jhje,0) je3 from zb t1 left join jh t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3
------解决方案--------------------
看不懂啊
1111
------解决方案--------------------