开发者

Adding total wages by group in PHP and MySQL

开发者 https://www.devze.com 2023-04-13 02:07 出处:网络
Through some queries and math, I have come to the point of gathering the total wages per employee on a given schedule. Each employee is assigned to a group, and now I\'m trying to get total wages by g

Through some queries and math, I have come to the point of gathering the total wages per employee on a given schedule. Each employee is assigned to a group, and now I'm trying to get total wages by group.

The table we're working with is called "schedules" and looks like:

SCH_ID | EMP_ID | GROUP_ID
55     | 1      | 7
55    开发者_开发技巧 | 2      | 7
55     | 3      | 8

So now I am staring at the data:

1 45.00
2 120.35
3 80.25

With 1,2,3 being the employee number and the amount of their wages to the right.

What I'm trying to accomplish is :

7 165.35
8 80.25

With 7,8 being the group numbers and the amount of total wages for that group. Maybe I have to join 2 queries or something, I dont know.

The queries and roundabout ways I've gotten to this point are sort of complicated so I hope this is enough information to help me come up with a way to do this... Any help would be greatly appreciated!


You can pull this off by using a quick JOIN and a GROUP BY operator on GROUP_ID. Something like so...

SELECT 
    schedules.GROUP_ID, 
    SUM(employees.wages) AS total_wages
FROM schedules
INNER JOIN employees 
    ON schedules.EMP_ID = employees.EMP_ID
WHERE 
    schedules.SCH_ID = 55
GROUP BY 
    schedules.GROUP_ID
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号