我有一张桌子,上面有这样的订单:
+----------+-------------+------------+
| order_id | customer_id | order_date |
+----------+-------------+------------+
| 404 | 16 | 2006-11-04 |
| 726 | 16 | 2007-11-01 |
| 440 | 16 | 2010-03-15 |
| 515 | 16 | 2011-04-29 |
| 539 | 16 | 2011-11-01 |
| 293 | 89 | 2001-08-28 |
| 156 | 89 | 2002-05-03 |
| 238 | 89 | 2003-09-15 |
| 299 | 28 | 2001-10-26 |
| 314 | 28 | 2004-03-18 |
| 142 | 79 | 2002-01-21 |
| 313 | 12 | 2004-03-11 |
| 388 | 12 | 2005-11-22 |
| 507 | 12 | 2007-03-27 |
| 286 | 14 | 2006-04-17 |
| 200 | 14 | 2008-05-25 |
| 130 | 14 | 2009-06-24 |
| 527 | 19 | 2011-05-14 |
| 298 | 76 | 2001-10-20 |
| 165 | 76 | 2002-07-27 |
| 212 | 11 | 2003-02-17 |
| 412 | 11 | 2006-08-02 |
| 423 | 11 | 2007-03-28 |
| 388 | 10 | 2005-01-07 |
| 390 | 65 | 2006-06-07 |
| 44 | 65 | 2008-01-07 |
| 269 | 16 | 2008-02-25 |
| 413 | 14 | 2007-12-01 |
| 675 | 14 | 2007-09-11 |
| 260 | 14 | 2009-10-20 |
+----------+-------------+------------+
现在我需要一个查询,它给我一个这样的列表:
+-------------+------------+------------+------+
| customer_id | date1 | date2 | days |
+-------------+------------+------------+------+
| 16 | 2006-11-04 | 2007-11-01 | 362 |
| 16 | 2007-11-01 | 2010-03-15 | 865 |
| 16 | 2010-03-15 | 2011-04-29 | 410 |
| 16 | 2011-04-29 | 2011-11-01 | 186 |
| 89 | 2001-08-28 | 2002-05-03 | 248 |
| 89 | 2002-05-03 | 2003-09-15 | 500 |
+-------------+------------+------------+------+
列 days 是每个客户从一个订单到下一个订单的时间跨度(以天为单位)。