表A(用户) 1对多 表B(收费记录)
id id
name Aid
money
现要查询表A每个用户的收费总和
sql1:
select
A.id
A.name
(select sum(B.money) from B where B.Aid = A.id) sumMoney
from A
sql2:
select
A.id
A.name
tab.sumMoney
from A left join (select sum(B.money) sumMoney,B.Aid from B group by B.Aid) tab on tab.Aid = A.id
sql1 和sql 2 哪个效率高点?
看不懂你写的什么。要我写是这样:
select a.id, a.name, sum(b.money) as sum where a left join b on a.id = b.aid group by a.id