﻿ calculate two columns in MySQL

# calculate two columns in MySQL

I have a MySQL database, where I have 3 columns:

Day_hours
Day_minutes
All_day_hours

I am gonna have some different form fields in a JSP page, where I can put in how many hours I work. On a day I work in different places, that means that I need fx to put in 5 * Day_hours, Day_minutes and All_day_hours. So the problem is that I want to calculate all the hours and minutes during a day. So if I fx worked:

1 job: 2 hours 15 minutes
2 job: 3 hours 45 minutes
3 job: 1 hours 10 minutes
4 job: 4 hours 40 minutes
5 job: 3 hours 15 minutes

So that means if I calculate the column "Day_minutes" it would give me the result 125. I would like that the 125 minutes is converted to hours, so the result would be 2 hours and 5 minutes. Afterwords the Day_hours and Day_minutes have to be addéd to the column Allday_hours. So Allday_hours is the sum of Day_hours + Day_minutes

so Fx in MySQL database there is the following information for an example day:

Day_hours      Day_minutes       Allday_hours
1 job                  2              15               2.15
2 job:                 3              45               3.45
3 job:                 1              10               1.10
4 job:                 4              40               4.40
5 job:                 3              15               3.15

So my question is, how do I calculate the Day_hours and Day_minutes to the Allday_hours, so the result in job 1 would be 2.15?

Have a good weekend.

You should not save something in Allday_hours as this is redundant information.

You can retrieve the data you want always (without problems) from the data you have. For example with

SELECT *, ((Day_hours*60 + Day_minutes)/60) AS Allday_hours FROM timedata...

No need to actually save them.

By the way I think it is rather odd that 2 hours plus 15 minutes add up to something like 2.15. So my query above computes something relative... if you really want to compute your value, you might use

SELECT *, (Day_hours + (Day_minutes / 100)) as Allday_hours FROM timedata

And if you really want to save this, you can use the calculations in an update statement like

UPDATE timedata SET Allday_hours = (Day_hours + (Day_minutes / 100))