两表查询,昏了!
zbcol1 col2 col3 je1 je2 je3a1 b1 c1 10 10 10a2 b2 c2 5 4 4a3 b3 c3 10 10 10....jhcol1 col2 col3 jhje a1 b1 c1 2 a2 b2 c2 3 ....需求:tabcol1 col2 col3 je1 je2 je3 jhjea1 b1 c1 10 10 10 2a2 b2 c2 5 4 4 3a3 b3 c3 10 10 10 0....zb表的数据多余jh表,我用left join 来连接,老是数据有问题。
select a.*,isnull(b.jhje,0) as jhjefrom 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,10insert into zb select 'a2','b2','c2',5, 4, 4insert into zb select 'a3','b3','c3',10,10, 10create 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',3goselect 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 2a2 b2 c2 5 4 4 3a3 b3 c3 10 10 10 0(3 行受影响)*/godrop table zb,jh
[解决办法]
select t1.col1, t1.col2, t1.col3, t1.je1, t1.je2, isnull(t2.jhje,0) je3from zb t1 left join jh t2 on t1.col1 = t2.col1 andt1.col2 = t2.col2 andt1.col3 = t2.col3
[解决办法]
看不懂啊
1111
[解决办法]