- SQL code
sql="select a.ID as DID," sql=sql&" IsNull(sum(b.Signing_AmountIN),0) as q1a," sql=sql&" IsNull(sum(c.Signing_AmountIN),0) as q2a" sql=sql&" from KL_Depart as a" sql=sql&" left join KL_Signing b" sql=sql&" on b.DepartID = a.ID" sql=sql&" and b.Signing_Stage = 3 and b.UnitID = "&KL_UnitID&" and b.Signing_UseDate between '"&CDate(BDate)&"' and '"&CDate(BDate2)&"'" sql=sql&" left join KL_Signing c" sql=sql&" on c.DepartID = a.ID" sql=sql&" and c.Signing_Stage = 3 and c.UnitID = "&KL_UnitID&" and c.ISCheckD = 1 and c.Signing_UseDate between '"&CDate(BDate)&"' and '"&CDate(BDate2)&"'" sql=sql&" where a.UnitID ="&KL_UnitID&" and a.ISWriteOFF = 0" sql=sql&" group by a.ID" sql=sql&" order by q1a desc"
如果我只Left join 一个的话,结果是正确的,但是我再增加了一个Left Join的话,结果q1a是正确结果的2倍,q2a是正确结果的3倍,请指点一下。
------解决方案--------------------
两种解决办法:
1.先将表两两连接,然后再将连接结果连接:
select * from(
select ... from .. a left join ... b on a.id=b.id
)a inner join (
select ... from ...a left join ... b on a.id=b.id
)b on a.id=b.id
2.只用一个连接,而另一个在select 子句中用子查询获得:
select .....,(select xx from c where id=a.id) as xx
from .... a left join ... b on a.id=b.id