写全了大概是这样吧: select isnull(sod.csocode,sod2.csocode) csocode,isnull(sod.iRowNo,sod2.iRowNo) irowno,isnull(so.ccusname,so2.ccusname) ccusname,inv.cinvname,。。 from rdrecord10 rd inner join rdrecords10 rds on rd.id=rds.id and rd.cvouchtype='10' inner join inventory inv on inv.bsale=1 and rds.cinvcode=inv.cinvcode left join (select csocode,irowno,ccusname from so_somain so inner join so_sodetails sod on so.id=sod.id) as sod on rds.isodid=cast(sod.isosid as varchar(20)) --如果rds.isodid存在就用这条关联 left join (select csocode,irowno,ccusname from so_somain so2 inner join so_sodetails sod2 on so2.id=sod2.id) as sod2 on rds.cmocode=sod2.csocode and sod2.cinvcode=rds.cinvcode --如果不存在就用这关联 where isnull(sod.csocode,sod2.csocode) is not null group by sod.csocode,sod2.csocode,sod.irowno,sod2.irowno,so.ccusname,so2.ccusname,inv.cinvname,inv.cinvstd
因为用isnull判断,在sod 有null 时才会去用 sod2,所以,sod not null 时,sod2的行不会被连接.
现在就是isodid与cmosode都存在时,就会有重复记录,因为两次left join都存在
你用了group by会去除重复的啊 ,或者distinct
left join so_sodetails sod on rds.isodid=cast(sod.isosid as varchar(20)) and sod.cinvcode=rds.cinvcode --如果rds.isodid存在就用这条关联 left join so_sodetails sod2 on rds.cmocode=sod2.csocode and sod2.cinvcode=rds.cinvcode --如果不存在就用这关联 条件也可以这样写,就是想合并
不会的. 因为你是判断,不会出现重复的,如果有重复,到别的地方找原因. 给个例子验证我的说法: create table t1(id int,col varchar(10)) insert into t1 select 1,'agbf' insert into t1 select 2,'fds' insert into t1 select 3,'vaisud' create table t2(id int,col int) insert into t2 select 1,234 create table t3(id int,col int) insert into t3 select 1,347145 insert into t3 select 2,874 go select a.*,isnull(b.col,c.col) col from t1 a left join t2 b on a.id=b.id left join t3 c on a.id=c.id /* id col col ----------- ---------- ----------- 1 agbf 234 2 fds 874 3 vaisud NULL(3 行受影响) */ go drop table t1,t2,t3 t2 和 t3 都有 id =1 的,但 t2 匹配了,t3 就不会被引用.
select isnull(sod.csocode,sod2.csocode) csocode,isnull(sod.iRowNo,sod2.iRowNo) irowno,isnull(so.ccusname,so2.ccusname) ccusname,inv.cinvname,。。
from rdrecord10 rd inner join rdrecords10 rds on rd.id=rds.id and rd.cvouchtype='10'
inner join inventory inv on inv.bsale=1 and rds.cinvcode=inv.cinvcode
left join (select csocode,irowno,ccusname from so_somain so inner join so_sodetails sod on so.id=sod.id) as sod on rds.isodid=cast(sod.isosid as varchar(20)) --如果rds.isodid存在就用这条关联
left join (select csocode,irowno,ccusname from so_somain so2 inner join so_sodetails sod2 on so2.id=sod2.id) as sod2 on rds.cmocode=sod2.csocode and sod2.cinvcode=rds.cinvcode --如果不存在就用这关联
where isnull(sod.csocode,sod2.csocode) is not null
group by sod.csocode,sod2.csocode,sod.irowno,sod2.irowno,so.ccusname,so2.ccusname,inv.cinvname,inv.cinvstd
left join so_sodetails sod2 on rds.cmocode=sod2.csocode and sod2.cinvcode=rds.cinvcode --如果不存在就用这关联
条件也可以这样写,就是想合并
不会的.
因为你是判断,不会出现重复的,如果有重复,到别的地方找原因.
给个例子验证我的说法:
create table t1(id int,col varchar(10))
insert into t1 select 1,'agbf'
insert into t1 select 2,'fds'
insert into t1 select 3,'vaisud'
create table t2(id int,col int)
insert into t2 select 1,234
create table t3(id int,col int)
insert into t3 select 1,347145
insert into t3 select 2,874
go
select a.*,isnull(b.col,c.col) col
from t1 a left join t2 b on a.id=b.id
left join t3 c on a.id=c.id
/*
id col col
----------- ---------- -----------
1 agbf 234
2 fds 874
3 vaisud NULL(3 行受影响)
*/
go
drop table t1,t2,t3
t2 和 t3 都有 id =1 的,但 t2 匹配了,t3 就不会被引用.