为多次出现的记录添加值

时间:2013-01-10 15:22:12

标签: sql database

我在数据库中查询2个表。如果用户在同一天多次显示,我想添加hours_worked字段的值。这就是我的开始:

SELECT name, date_worked, hours worked
FROM users u
INNER JOIN records r
ON u.id = r.users_id
.......

就我而言。有关如何执行此操作的SQL语法的任何帮助都会很棒吗?

1 个答案:

答案 0 :(得分:1)

使用SUM

汇总它
SELECT name, date_worked, SUM(`hours worked`) AS totalHoursWorked
FROM users u
     INNER JOIN records r
       ON u.id = r.users_id
GROUP BY name, date_worked