用于以下查询:
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
答案 0 :(得分:0)
如果在J
中有3条记录,并且ID
中的4条记录在结果集中的JOIN之后得到3 * 4 = 12条记录,则应立即将SUM和COUNT从不同的表中分开,这样您就可以了得到错误的总和和数。例如:
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