简体   繁体   中英

MySQL SUM values of difference between two columns

I have two columns in one of my tables called TIME_OUT and TIME_IN. These are both decimal values. I basically want to sum the difference of these columns. I have the query below that outputs the difference with no issue, but I'd like to then SUM the difference:

SELECT (TIME_IN-TIME_OUT) AS DIFF FROM TABLE_timelogs WHERE YEAR(LOG_DATE) = YEAR(NOW());

+------+
| DIFF | 
+------+
| 10.0 |
|  4.0 |
|  3.0 |
+------+

Is there a way to integrate SUM into the query so that the ultimate output is 17.0? Thank you in advance!

Add SUM to your query so the query reads:

SELECT SUM(TIME_IN-TIME_OUT) AS DIFF 
FROM TABLE_timelogs 
WHERE YEAR(LOG_DATE) = YEAR(NOW());

This adds the contents of the column 'diff'.

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