MySQL-SUM DateTime? [英] MySQL - SUM DateTime?

查看:210
本文介绍了MySQL-SUM DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要日期时间总和,但是我不知道该怎么做.

I need sum datetime values, but I don't know how I can do this.

我有桌子:

我的查询

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

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

But, when I run that query, my result is:

为什么不正确?

编辑

我在表格中的日期时间更改了格式:

I change the format from datetime to time in my table:

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

So, I need sum the time values.

编辑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:)

Thanks for @Newbee Dev and @EhsanT :)

推荐答案

首先:将日期时间格式化为正好时间

First: Format your datetime into just time

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

然后将其转换为仅几秒钟

then convert it into just a seconds

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

转换后,您现在可以将它们相加

after converting it you can now sum them

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

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

finally the result is now ready and just return it's format as time format

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

示例结果

这篇关于MySQL-SUM DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆