2006-10-24
左连接left join 的空数据
select
T1.serialno,t1.dwmonth,t2.dwmonth,T1.balance,t2.balance as ReportItem
from
receive_badloan T1 left join a_contract T2 on
t1.dwmonth=t2.dwmonth and t2.dwmonth='2005/10/31' and
t1.serialno = t2.serialno and
t1.businesscurrency = t2.businesscurrency where operateorg like '1011%'.
上面这句sql的要求是:最终显示的余额(balance)来自t2,serialno,dwmonth,businesscurrency是t2的三个主键。看起来这样写好像没问题,但是查询结果如下:
serialno t1.dwmonth t2.dwmonth t1.balance reportItem
111998060004 2005-09-30 303697.6
111998060004 2005-10-31 2005-10-31 278697.6 278697.6
,t2.balance有为空的值。查看原因发现 t1.dwmonth=t2.dwmonth为多余的条件,因为现在的要求是查询dwmonth为2005/10/31的余额,如果t1.dwmonth=t2.dwmonth了,则2005-09-30去关联t2 中 t2.dwmonth='2005/10/31'的数据当然 没得关联所以,t2.balance为空。
正确sql应该为:
select
T1.serialno,t1.dwmonth,t2.dwmonth,T1.balance,t2.balance as ReportItem
from
receive_badloan T1 left join a_contract T2 on
t2.dwmonth='2005/10/31' and
t1.serialno = t2.serialno and
t1.businesscurrency = t2.businesscurrency where operateorg like '1011%'
正确结果如下:
111998060004 2005-09-30 2005-10-31 303697.6 278697.6
111998060004 2005-10-31 2005-10-31 278697.6 278697.6
T1.serialno,t1.dwmonth,t2.dwmonth,T1.balance,t2.balance as ReportItem
from
receive_badloan T1 left join a_contract T2 on
t1.dwmonth=t2.dwmonth and t2.dwmonth='2005/10/31' and
t1.serialno = t2.serialno and
t1.businesscurrency = t2.businesscurrency where operateorg like '1011%'.
上面这句sql的要求是:最终显示的余额(balance)来自t2,serialno,dwmonth,businesscurrency是t2的三个主键。看起来这样写好像没问题,但是查询结果如下:
serialno t1.dwmonth t2.dwmonth t1.balance reportItem
111998060004 2005-09-30 303697.6
111998060004 2005-10-31 2005-10-31 278697.6 278697.6
,t2.balance有为空的值。查看原因发现 t1.dwmonth=t2.dwmonth为多余的条件,因为现在的要求是查询dwmonth为2005/10/31的余额,如果t1.dwmonth=t2.dwmonth了,则2005-09-30去关联t2 中 t2.dwmonth='2005/10/31'的数据当然 没得关联所以,t2.balance为空。
正确sql应该为:
select
T1.serialno,t1.dwmonth,t2.dwmonth,T1.balance,t2.balance as ReportItem
from
receive_badloan T1 left join a_contract T2 on
t2.dwmonth='2005/10/31' and
t1.serialno = t2.serialno and
t1.businesscurrency = t2.businesscurrency where operateorg like '1011%'
正确结果如下:
111998060004 2005-09-30 2005-10-31 303697.6 278697.6
111998060004 2005-10-31 2005-10-31 278697.6 278697.6
左连接并非主键全部要等值起来!
发表评论
- 浏览: 394579 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
Gmail
共 8 张
共 8 张
最新评论
-
JIRA不完全手册
楼主,发一份完整的资料给我吧 xwj1003@yahoo.com.cn
-- by volking -
java encoding参考
Good ,thanks.
-- by zuowei -
有多少异常可以重来
我今天也遇到这个问题,才搜到这个帖子~ 还没有解决,等恢复哦
-- by javamanlcy007 -
有多少异常可以重来
这个错误解决了,又抱了别的错误~
-- by iceworld4143 -
有多少异常可以重来
解释不太懂,我也遇到这个问题了,可是我没用ant。 :cry: 等回 ...
-- by iceworld4143






评论排行榜