gpt4 book ai didi

MySQL - 求和日期时间?

转载 作者:可可西里 更新时间:2023-11-01 06:42:30 27 4
gpt4 key购买 nike

我需要求和日期时间值,但我不知道该怎么做。

我有 table :

enter image description here

我的查询:

SELECT 
SUM(h.dtplay) AS Time
FROM
tblhistory AS h,
tblgame AS g
WHERE
h.idgame = g.id AND
g.description = gameName;

但是,当我运行该查询时,我的结果是:

enter image description here

为什么不正确?

编辑

我将表格中的格式从日期时间更改为时间:

enter image description here

所以,我需要对时间值求和。

编辑 2

正确的查询:

SELECT 
SEC_TO_TIME(SUM(TIME_TO_SEC(h.DtPlay))) AS Time
FROM
tblhistory AS h,
tblgame AS g
WHERE
h.idgame = g.id AND
g.description = gameName;

感谢@Newbee Dev 和@EhsanT :)

最佳答案

首先:将您的日期时间格式化为时间

Date_format(DtPlay, '%h:%i:%s')

然后将其转换为秒

Time_to_sec(Date_format(DtPlay, '%h:%i:%s')

转换后你可以对它们求和

Sum(Time_to_sec(Date_format(DtPlay, '%h:%i:%s'))

最后结果已经准备好,只需将其格式返回为时间格式

SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( DtPlay ) ) ) FROM tblhistory

Sample Result

关于MySQL - 求和日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40456972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com