简体   繁体   中英

How to calculate Total hours using query in mysql?

I have field named "Time" in my table. I want to calculate total hours.

Time
3:00:00
4:00:00
5:00:00

Output should be 2hr. How can I achieve this? Please help me to solve it.

I tried below query but it did not work as expected.

SELECT  SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum  
FROM YourTableName

如果需要两个值之间的最大时间跨度,请尝试:

SELECT MAX(timeSpent) - MIN(timeSpent) FROM YourTableName

suppose your table have an id as a primary key auto incremental, so you query will be:

SELECT FLOOR(sum( abs( SEC_TO_TIME(TIME_TO_SEC(t1.`timeSpent`))  -  SEC_TO_TIME(TIME_TO_SEC(t2.`timeSpent`)) )/10000)) as total 
FROM YourTableName t1
inner join YourTableName t2 
on t1.id = t2.id + 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM