SUM DateTime?

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

I have a table:

enter image description here

And my request:

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 this query, my result is:

enter image description here

Why is this wrong?

EDIT

I am changing the format from date and time in my table:

enter image description here

So, I need to sum the time values.

EDIT 2

Correct request:

 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; 

Thanks for @Newbee Dev and @EhsanT :)

+5
source share
2 answers

First: Format the date and time at the moment

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

then convert it in seconds

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

after converting it you can sum them

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

Finally, the result is now ready and simply returns its format as a time format

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

Result Example

+9
source

Can you give a little more information about what you are trying to do. Because I can’t think of a scenario where you would like to summarize the years. The result you get seems right to me, because you can divide it into seconds, minutes, hours, days, month, and years.

-1
source

Source: https://habr.com/ru/post/1259386/


All Articles