两张桌子像
表格1
+----+-------+-----+
| id | sname | sal |
+----+-------+-----+
| 1 | X | 100 |
| 2 | Y | 200 |
| 3 | Z | 400 |
+----+-------+-----+
表2
+----+-------+-----+
| id | sname | sal |
+----+-------+-----+
| 1 | A | 500 |
| 2 | B | 200 |
| 3 | C | 400
| 4 A 100
+----+-------+-----+
两个表都有关系船 id 列
我需要同时通过table1.sname计算与table2匹配的总和sal组
输出像
+-------+-------+---------------------
| Table1.sname | Table2.sname | sum |
+-------+-------+-----+ ----------------
| A | W | 600 |
| B | Y | 200 |
| B | F | 300 |
| C | Z | 400 |
+-------+-------+----------------------
select sum(sal),a.sname,b.sname
from table1 a,
(select id,sname from table2 group by sname,id) as b
where a.id=b.id
group by a.sname,b.sname;
但它没有给出正确的o/p