对于以下查询:
select sum(j.credits) as credits, count(id.redo) as redos
from operator o inner join jobdetails j on j.opid=o.id
inner join imagedetails id on id.opid=o.id
and im.redo=y group by o.id
对于以下查询:
select sum(j.credits) as credits, count(id.redo) as redos
from operator o inner join jobdetails j on j.opid=o.id
inner join imagedetails id on id.opid=o.id
and im.redo=y group by o.id
J
如果您有 3 条记录和 4 条记录,您应该尽快将 SUM 和 COUNT 从不同的表中分离出来,ID
您在结果集中 JOIN 后得到 3*4=12 条记录,因此您得到错误的总和和计数。例如:
select o.id, j.sum_credits as credits, id.count_redo as redos
from operator o
inner join
(select opid, sum(credits) sum_credits from jobdetails group by opid) j
on j.opid=o.id
inner join
(select opid, count(redo) count_redo from imagedetails
where redo=y group by opid) id
on id.opid=o.id