user2082882
user2082882

Reputation: 23

Calculate Overtime Workhours and Updating the overtime to table

I have table columns workhours and overtime, and I would like to do a SQL or PHP query that checks if the workinghours are over 8 hours and if so it UPDATEs the worked overtime to overtime column.

Here's my table:

id   workinghours    overtime
1,   4.79,           ---
2,   8.73,           ---
3,   7.97,           ---  

For example on the second row there is 8.73 hours of work and so it would update the 0.73 hours to the overtime column.

Upvotes: 0

Views: 940

Answers (3)

valex
valex

Reputation: 24144

update YourTable 
       set overtime=workinghours-8 
           where workinghours>8

Upvotes: 0

Suhel Meman
Suhel Meman

Reputation: 3852

query :

update work set overtime= (case when working > 8 then (working - 8) else null end);

check SQL Fiddle Demo

Upvotes: 1

Ranty
Ranty

Reputation: 3362

Try something like this:

update mytable
    set overtime =
        case when workinghours > 8 then workinghours - 8 else 0 end
;

Upvotes: 1

Related Questions