两个表 表1(此表是数量合计) 商品id 数量 a 10 b 20 c 10 表2(此表是明细) 商品id 数量 a 1 a 9 b 10 b 10 c 5 c 4 表2的商品c合计是9, 而表1的数量10, 怎么把c找出来,形成如下的结果: 商品 表1.数量 表2.数量 c 15 14 ......
select a.id,a.amount as amount1,sum(b.amount) as amount2 from t1 a inner join t2 b on a.id=b.id where exists(select t2.id,sum(amount) from t2 where t2.id=a.id group by t2.id having sum(t2.amount)<>a.amount ) group by a.id,a.amount