次のクエリの場合:
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 つのレコードがあるID
場合、結果セットの 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