テーブル A uid 1 1 101 100
create temporary table Aselect 1 as uid, 1 as mid, 100 as cashunion select 2, 2, 105union select 3, 3, 98union select 4, 4, 55union select 5, 5, 60union select 6, 6, 70;create temporary table Bselect 1 as uid, 1 as mid, 101 as cash, 100 as numberunion select 2, 1, 100, 100union select 3, 2, 105, 200union select 4, 2, 105, 300union select 5, 3, 60, 300union select 6, 3, 98, 300;select uid,mid, cash, (select sum(number) from B where mid=A.mid and cash=A.cash) from A
4 4 55
5 5 60 6 6
テストを書くdata クエリ命令を書くよりも 10 倍以上の時間がかかります
これが私が望むものです
左結合から数値として a.*,if(t.number is null,0,t.number) を選択します
(select b.mid,b.cash,sum(b.number) as number from b group by Mid,cash) as t
on t.mid=a.mid and t.cash=a.cash
select a.uid, a.mid, a.cash, sum(b.number) from testa as a left join testb as b on a.mid=b.mid and a.cash = b.cash group by a.mid, a.cash