我有一个多对多表 p_to_t
+----------+-----------+-----------+
| p | t | weight |
+----------+-----------+-----------+
| 1 | bob | 40 |
+----------+-----------+-----------+
| 2 | sue | 24 |
+----------+-----------+-----------+
| 3 | bob | 90 |
+----------+-----------+-----------+
| 4 | joe | 55 |
+----------+-----------+-----------+
| 5 | bob | 33 |
+----------+-----------+-----------+
我正在查询值 t WHERE p IN(1,2,4,5),同时对每个 t 值的权重求和。
此查询:"SELECT t, SUM(weight) AS sWeight FROM p_to_t WHERE p IN(1,2,4,5)";
只需将所有总和添加到所选的第一个“t”,例如;
+-----------+-----------+
| t | weight |
+-----------+-----------+
| bob | 152 |
+-----------+-----------+
当我想要的是:
+-----------+-----------+
| t | weight |
+-----------+-----------+
| bob | 97 |
+-----------+-----------+
| joe | 55 |
+-----------+-----------+
想法?