MySQL的减法时间在不同机器上不一致

发布于 2024-07-09 12:30:07 字数 899 浏览 11 评论 0原文

我的MySQL查询结构如下:

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

开始和结束时间的数据分别是1970-01-01 07:00:00和1970-01-01 19:00:00。

在我的本地计算机上,此查询返回:

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

但是在远程计算机(生产)上,它返回

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

关于为什么会发生这种情况以及如何修复它的任何想法?

两组数据是相同的,而且据我所知,两个 MySQL 安装也是相同的。

任何帮助深表感谢。

更新:

MySQL 的版本似乎略有不同:5.0.27-community-nt 与 5.0.51b-community-nt。 这很可能就是原因。

djt 提出了一个很好的观点,即 Bill 的解决方案没有考虑会议记录。 而且这个 djt 的解决方案并不完全是我所需要的。

所以我想问题已经变成了如何减去两次,包括分钟,这样:

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

等等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾听心声的旋律 2024-07-16 12:30:07

我会这样做:

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;

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;
如果没有 2024-07-16 12:30:07

通常,相同的方法会导致不同的输出,给人的印象是它是不同的服务器配置或版本。 我真的无法提供帮助,但可能值得在您的查询中尝试以下方法:

选择时间(c.start_time), 
         时间(c.结束时间), 
         TIMEDIFF(TIME(c.end_time), TIME(c.start_time)) as 'opening_hours' 

  来自 my_shop c; 
  

这将为您提供开放时间、分钟和秒的格式化时间戳。

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.

乖不如嘢 2024-07-16 12:30:07

还有比这更好的解决方案吗:

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;

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;
喜你已久 2024-07-16 12:30:07

我使用的是 mysql 版本 5.7.37,我遇到了保存问题。 我注意到减法总是乘以 40(100 和 60 之间的差)。我检查了数据,发现当使用 sunstraction 时,我的 mysql 版本将时间(或在你的情况下)更改为整数。

我的意思是它将 07:00:00 更改为 70000,将 19:00:00 更改为 190000,然后将它们吸收。(想象一下实现此方法的开发人员是多么愚蠢)。
我认为在你的情况下,你在一个地方有一个正确的(已修复错误的)版本,在另一个地方有错误的版本。

Im using the mysql verion 5.7.37 and I have the save problem. I noticed that the substraction is allways off by a multiplication of 40 ( the difference between 100 and 60) I checked the data and I found that when using sunstraction my version of mysql change the time (or in your case ) to integere.

I mean it changes 07:00:00 to 70000 and 19:00:00 to 190000 and then suntracts them.( just imagine how dumm the developer who implemented this method is).
I think in your case you have a right( bug-fixed) vesrion on oneplace and the buggy one on another.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文