简体   繁体   English

如果记录不是唯一的,则从表中获取唯一的记录+总和

[英]Get unique records from a table + sum if the record is not distinct

I have a table in mysql like below : 我在mysql中有一个如下表:

Table name : Emp 表名:Emp

Emp Name | Time 
_______________
ABC      | 1
ABC      | 2
ABC      | 3
PQS      | 4
XYZ      | 5

I want the output as below 我想要以下输出

Emp Name | Time 
_______________
ABC      | 6
PQS      | 4
XYZ      | 5

As you can see only the unique Emp names are returned and the time is added for each instance of Emp name. 如您所见,仅返回唯一的Emp名称,并且为Emp名称的每个实例添加时间。

This might help: 这可能会有所帮助:

SELECT
   EmpName,
   SUM(Time) AS Total
FROM
  table1
GROUP BY
   EmpName

This is a rudimentary aggregate SUM() grouping by Emp Name : 这是按Emp Name分组的基本汇总SUM()

SELECT 
  EmpName,
  SUM(Time) AS Time
FROM Emp
GROUP BY EmpName
ORDER BY EmpName

Review the MySQL documentation on aggregate functions and the use of GROUP BY clauses. 查看有关聚合函数和GROUP BY子句使用的MySQL文档

Note, if your column name is actually Emp Name with the space, you will need to quote it with backticks as in: 注意,如果您的列名称实际上是带空格的Emp Name ,则需要用反引号将其引起来,如下所示:

SELECT 
  `Emp Name`,
  SUM(Time) AS Time
FROM Emp
GROUP BY `Emp Name`
ORDER BY `Emp Name`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM