我有一张这样的桌子:
+----+---------+----------+ | id | group | value | +----+---------+----------+ | 1 | GROUP A | 0.641028 | | 2 | GROUP B | 0.946927 | | 3 | GROUP A | 0.811552 | | 4 | GROUP C | 0.216978 | | 5 | GROUP A | 0.650232 | +----+---------+----------+
如果我执行以下查询:
SELECT `id`,SUM(`value`) AS `sum` FROM `test` GROUP BY `group`;
我显然得到:
+----+-------------------+ | id | sum | +----+-------------------+ | 1 | 2.10281205177307 | | 2 | 0.946927309036255 | | 4 | 0.216977506875992 | +----+-------------------+
但我需要像这样的桌子:
+----+-------------------+ | id | sum | +----+-------------------+ | 1 | 2.10281205177307 | | 2 | 0.946927309036255 | | 3 | 2.10281205177307 | | 4 | 0.216977506875992 | | 5 | 2.10281205177307 | +----+-------------------+
其中相加行被明确地重复.
有没有办法获得这个结果,而不使用多个(嵌套)查询?