Posted By

bradless on 02/19/10


Tagged

mysql date difference status change Subquery datediff


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

dantreacy
virendrakryadav


calculate the difference between dates in one columns within the same table


 / Published in: MySQL
 

I use something similar to the example to get the difference between statuses in a status log table that I have. It comes in handy when determining the number of days between status changes. This works in MySQL 5.0.

  1. SELECT DATEDIFF(
  2. (SELECT DATE(date_field) FROM table WHERE id = '1' AND status_id = '2' ORDER BY date_field DESC LIMIT 1),
  3. (SELECT DATE(date_field) FROM table WHERE id = '1' AND status_id = '1' ORDER BY date_field DESC LIMIT 1)
  4. ) AS difference

Report this snippet  

You need to login to post a comment.