我正忙着使用旧样式语法将查询转换为新的连接语法.我的查询的本质如下:
原始查询
SELECT i.* FROM InterestRunDailySum i,InterestRunDetail ird,InterestPayments p WHERE p.IntrPayCode = 187 AND i.IntRunCode = p.IntRunCode AND i.ClientCode = p.ClientCode AND ird.IntRunCode = p.IntRunCode AND ird.ClientCode = p.ClientCode
新查询
SELECT i.* FROM InterestPayments p INNER JOIN InterestRunDailySum i ON (i.IntRunCode = p.IntRunCode AND i.ClientCode = p.ClientCode) INNER JOIN InterestRunDetail ird ON (ird.IntRunCode = p.IntRunCode AND ird.IntRunCode = p.IntRunCode) WHERE p.IntrPayCode = 187
在此示例中,“原始查询”返回46行,其中“新查询”返回超过800
有人可以向我解释一下这个区别吗?我会假设这些查询是相同的.
解决方法
问题是您加入InterestRunDetail.您将加入IntRunCode两次.
正确的查询应该是:
SELECT i.* FROM InterestPayments p INNER JOIN InterestRunDailySum i ON (i.IntRunCode = p.IntRunCode AND i.ClientCode = p.ClientCode) INNER JOIN InterestRunDetail ird ON (ird.IntRunCode = p.IntRunCode AND ird.ClientCode = p.ClientCode) WHERE p.IntrPayCode = 187