简体   繁体   中英

Get average time between several dates for one entry

I am not really sure how to explain this, but basically what I am trying to do is get the average time that it takes between a set of dates. This is what the database table looks like

id | offer_id | user |    date    | date_completed
----------------------------------------------------
1  |   123    | test | 1352265988 |   1352265995
2  |   123    | admin| 1352266004 |   1352266022
3  |   123    | kira | 1352264754 |   1352271946

I need to get the average time between the 2 timestamps and calculate them together so I can echo it out in my code. I am sure this would be done with a foreach statement, but I have no idea how to go about doing it. If there is anyone that can point me in the right direction that would be great!

Unless I'm missing something, you can just do it in a single query:

SELECT AVG(date_completed - date)
FROM myTable

Use the SQL-function AVG for each of the colums (date and date_completed). Than, you can just substract each average time and you will get your result you are searching for.

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