简体   繁体   中英

How to sum Total Time for each user in a while loop PHP

I have this table (for example)

    id      userId     totalHours      isOt
    1       1          0830            1
    2       4          0630            0
    3       1          0730            0
    4       2          0900            1

I need to find the totalHours for each user but they should be shown once for each user (Group By)

And then, if the totalHours was awarded as OT (over time == 1) the difference between totalHours minus 0800 hours should be summed up too and shown in another column.

I have tried to join this table with user table but the loop seems so wrong which ended up totalling all last values in every loop to another.

Hope there will be anyone who can help and your help is appreciated

Do it in your database query, not PHP:

SELECT UserId,
       SUM(totalHours) AS totalTime,
       SUM(IF(isOt, totalhours-800, 0)) AS totalOt
FROM yourTable
GROUP BY UserId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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