我需要帮助在MySQL中编写求和查询

时间:2009-07-27 19:29:17

标签: mysql sum

我有一个看起来像这样的表:

posid    sales      eid   

  1       20         001
  1       20         002
  1       30         001
  2       30         001
  1       30         002
  2       30         001
  1       30         002
  2       20         002
  2       10         002

我想写一个查询,它会给我特定pos上每位员工的销售总额。结果必须是这样的。

pos id    emp id     sales

  1       001        50
  1       002        80
  2       001        60
  2       002        30

我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用group by

select t.posid
       , t.eid
       , sum(t.sales) as sales_by_posid

from   mytable t

group by t.posid, t.eid

order by sales_by_posid desc

答案 1 :(得分:0)

SELECT
    posID, empID, sum(sales)
FROM your_table
GROUP BY posID, empID
ORDER BY posID, empID

以下是教程组:http://www.tizag.com/mysqlTutorial/mysqlgroupby.php