views:

388

answers:

3

I have a MySQL query structured as follows:

SELECT time(c.start_time),
       time(c.end_time),
       time(c.end_time) - time(c.start_time) as 'opening_hours' 
FROM my_shop c;

The data in start and end time is 1970-01-01 07:00:00 and 1970-01-01 19:00:00 respectively.

On my local machine this this query returns:

| 07:00:00 | 19:00:00 | 12 |

However on a remote machine (production) it is returning

| 07:00:00 | 19:00:00 | 120000 |

Any ideas as to why this might be happening and how to fix it?

Both sets of data are identical and too the best of my knowledge both MySQL installations are identical.

Any help is much appreciated.

Update:

It would seem that the versions of MySQL are slightly different: 5.0.27-community-nt versus 5.0.51b-community-nt. This is most probably the reason why.

djt has raised a good point in that Bill's solution does not take into account minutes. As well as this djt's solution is not quite what i need.

So i guess the question has morphed into how to subtract two times including minutes such that:

1970-01-01 19:00:00 - 1970-01-01 07:00:00 = 12 
1970-01-01 19:00:00 - 1970-01-01 07:30:00 = 11.5
1970-01-01 19:00:00 - 1970-01-01 07:45:00 = 11.25

etc.

+1  A: 

I'd do it this way:

SELECT TIME(c.start_time),
       TIME(c.end_time),
       EXTRACT(HOUR FROM TIMEDIFF(TIME(c.end_time), TIME(c.start_time)))
         AS 'opening_hours' 
FROM my_shop c;
Bill Karwin
This will work on the desired machine. Ideally i do not want to change code but i suppose if this is the only solution then so be it. I would still really like to know why this is happening though it is quite perplexing.
Abarax
I am not sure. I would suggest verifying the versions of MySQL on each machine. "SELECT VERSION();"
Bill Karwin
Sorry bill, I didn't see your answer before I submitted. Our solutions are similar but I don't believe that yours will display part hours (if the end date was 19:30 for example).
djt
You are right, it will only show whole hours as an integer. But that's what the OP was asking for.
Bill Karwin
+1  A: 

Usually the same methods causing different output gives the impression that it is either a different server configuration or version. I'm not really able to help with that but it may be worth trying the below method in your query instead:

SELECT TIME(c.start_time),
       TIME(c.end_time),
       TIMEDIFF(TIME(c.end_time), TIME(c.start_time)) as 'opening_hours'

FROM my_shop c;

This will give you a formatted timestamp of the opening hours, minutes and seconds.

djt
Interesting, this yields "12:00:00" which i guess is what my query is returning on that specific machine without the colons. Perhaps there is a date formatting setting in MySQL's configuration that is different. Any ideas?
Abarax
A: 

Is there a better solution than this:

SELECT EXTRACT(HOUR FROM TIMEDIFF(TIME(c.end_time), TIME(c.start_time))) 
+ ((EXTRACT(MINUTE FROM TIMEDIFF(TIME(c.end_time), TIME(c.start_time))))/60)
FROM my_shop c;
Abarax
Do you know you can do EXTRACT(HOUR_MINUTE FROM ...)?
Bill Karwin