求sql语句,mysql多对多查询

2025-03-22 10:38:35
推荐回答(3个)
回答1:

select *
from
(select id,sum(money) as mm from a表 group by id) aaa,
(select id,sum(money) as nn from b表 group by id) bbb
where aaa.id=bbb.id and aaa.mm=bbb.nn;
-----------------------------
说明:
先用语句,得到aaa,bbb两个临时表,里面是(ID,钱的求和);
然后叠加一个查询,从表aaa,表bbb中,用条件,筛选出需要的记录(ID相等,钱求和相等);mm,nn是我为了字段查看方便,设置的两个临时字段名。
-----------------------------
如果你是用workbench,语句正确执行要写成一行。

回答2:

select a.id from
(select id,sum(money) m from a group by id) a
left join
(select id,sum(money) m from b group by id) b
on (a.id=b.id and a.m=b.m)

这个你试试

回答3:

" 相加也相等的",这是什么意思 没听明白