0

I have these two rows:

id value created
1  10    2012-01-05
2  12    2012-02-13

How do i find the number of days in between these two consecutive records (based off of their created field)?

Thanks!

Kitsune
  • 9,101
  • 2
  • 25
  • 24
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • A bit more info needed, what would be the input and expected output? Two id's in, the date difference out, or just one id and get the diff between that and next or...? – Joachim Isaksson Feb 19 '12 at 21:38
  • @JoachimIsaksson two ids and then the date difference out – benhowdle89 Feb 19 '12 at 21:39
  • @MarkBaker beat me to it, see his answer :) – Joachim Isaksson Feb 19 '12 at 21:41
  • @JoachimIsaksson, and someone beat MarkBaker to it as well. :) possible duplicate of [MySql difference between two timestamps in days?](http://stackoverflow.com/questions/2546053/mysql-difference-between-two-timestamps-in-days) – bzlm Feb 19 '12 at 21:43
  • There's very few questions asked here that haven't been answered time and again, guess I should have checked the "related" list of questions more closely – Mark Baker Feb 19 '12 at 21:48

1 Answers1

3

MySQL's DATEDIFF function should help

SELECT DATEDIFF(a.created,b.created)
  FROM table a,
       table b
 WHERE a.id = 1
   AND b.id = 2
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Correct since the column is `DATE`, but just a note so no one does the same with `DATETIME` without reading your documentation link, `DATEDIFF` on `DATETIME`s from one second before midnight to midnight is 1 day since it only uses the `DATE` part. – Joachim Isaksson Feb 19 '12 at 21:48