0

我希望通过同一个表中的相同 ID 将值总和到我的数据库中。数据库中的表:

| ID | Value_o | Value_t | Value_tt |
|  1 |   40    |    20   |   10     |

询问:

SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT 
WHERE ID IN(1, 1)

现在输出ID:

| Value_o | Value_t | Value_tt |
|   40    |    20   |   10     |

但我想要:

| Value_o | Value_t | Value_tt |
|   80    |    40   |   20     |

我想在没有 JOIN 的情况下得到这个输出。

谢谢!

PS。对不起我的英语不好:/

4

3 回答 3

1

也许这就是你正在寻找的:

SELECT 
  SUM(Value_o) AS Value_o, 
  SUM(Value_t) AS Value_t, 
  SUM(Value_tt) AS Value_TT 
FROM
  (
  SELECT ID, Value_o, Value_t, Value_tt FROM Table1 
  UNION ALL 
  SELECT ID, Value_o, Value_t, Value_tt FROM Table1
  ) Table2 
WHERE ID IN(1, 1);

演示

于 2013-03-17T21:53:24.453 回答
0

尝试这个:

SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT 
FROM TABLE
GROUP BY ID
HAVING ID = 1
于 2013-03-17T21:44:31.973 回答
0

MySQLin运算符不能以这种方式工作。即使您在集合中有多个值,它也不会复制结果的行。

如果您想多次获得所有行,则必须使用union all并对其求和

SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT 
from (select * from mytable union all select * from mytable) t
WHERE ID IN (1)
于 2013-03-17T22:00:20.200 回答