两个类似的mysql查询如何显示不同的结果

时间:2014-07-29 06:46:12

标签: mysql

我有两个MySQL查询,我试图用来平衡数据库并找到问题。

查询1预付总计286940.99和查询2总计288645.3,这是1,704.31不同(预先未分配也是错误的)。

我遇到的问题是它们都是相同的列,唯一的区别是查询2有一个左连接来连接另一个表。左连接不应该影响主表。

以下是我的查询

查询1

  SELECT sum(advance) as advance, sum(advance_unallocated) as advance_unallocated FROM     `deals` 

查询2

SELECT 

pb.id as pbid,
d.id as did,
sum(d.advance) as advance,
sum(d.advance_unallocated) as advance_unallocated,
sum(pb.payments) as pb_payment,
sum(pb.payments) - sum(d.advance) - sum(d.advance_unallocated) as diff,
pb.payment_method as p_payment_method 

FROM `deals` d 

LEFT OUTER JOIN `payment_balance` pb on d.id = pb.link_id and pb.table_name = 'deals' 

1 个答案:

答案 0 :(得分:0)

这是因为LEFT JOIN返回的不是您最初想到的[0, 1]行数,而是所有与连接条件匹配的行,或者填充的行{{1}否则。

在您的特定情况下,有一行或多行符合连接条件“意外”。