I have this table in mysql
Date one | Date tow |
2012-05-20 | 2012-05-04 | = 16 days
2012-05-12 | 2012-05-08 | = 4 days
= 20 days
and i want to select difference between two dates and then sum all days.
I have this table in mysql
Date one | Date tow |
2012-05-20 | 2012-05-04 | = 16 days
2012-05-12 | 2012-05-08 | = 4 days
= 20 days
and i want to select difference between two dates and then sum all days.
If you should do it in MySQL, you could use DATEDIFF function.
SELECT DATEDIFF(dateone, datetwo) AS d FROM tablename
and then you could aggregate this result the way you want, example
SELECT SUM(DATEDIFF(dateone, datetwo)) AS s FROM tablename
You can do it also in PHP after fetching the dates
SELECT SUM(DATEDIFF(`Date one`, `Date tow`)) FROM `Table`
See DATEDIFF()
and SUM()
.
Note that in MySQL, you do not need to use GROUP BY
in order to be able to use the aggregate function SUM()
. In that case, MySQL will regard all rows as a single group.
You can try the below query. hope it will sort out the issue.
SELECT SUM(DATEDIFF(DATE1,DATE2)), FROM TABLE
You can use the DATEDIFF
function in mysql.
SET @runtot:=0;
SELECT DATEDIFF(one,tow) AS diff, (@runtot := @runtot + DATEDIFF(one,tow)) AS runningsum
FROM table