MySQL选择多列和SUM列

时间:2013-11-14 19:57:20

标签: mysql

Lets说我有一张这样的桌子 -

 id  |  name  |  salary
 01  |  ABCD  |  1000
 02  |  EFGH  |  2000    
 03  |  IJKL  |  3000

现在可以得到这样的结果集吗?

id  |  name  |  salary | SUM
01  |  ABCD  |  1000   | 6000
02  |  EFGH  |  2000
03  |  IJKL  |  3000

我试过SELECT id, name, salary, SUM(salary) FROM table,但它只给了我一行。有没有办法让所有行都有一个额外的列?

2 个答案:

答案 0 :(得分:1)

尝试:

SELECT id, name, salary, SUM(salary) AS sum FROM table
编辑:想想我被误解了。也许这就是你要找的东西:

SELECT id, name, salary (SELECT SUM(salary) FROM table) AS sum FROM table

答案 1 :(得分:1)

SELECT
  id,
  name,
  salary,
  (SELECT SUM(salary) FROM table)
FROM
  table