mysql计算天数

时间:2012-05-18 14:23:20

标签: mysql subquery

我的表格中有这样的订单:

+----------+-------------+------------+
| 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  |
+-------------+------------+------------+------+

列天数是每个客户从一个订单到下一个订单的天数。

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

SELECT A.customer_id, 
    A.date as date1, 
    B.date as date2, 
    datediff(B.date, A.date) as days
FROM (select @curRowA := @curRowA + 1 AS row_number, 
        customer_id, 
        date from 
        orders 
    ORDER BY customer_id asc, date desc) A
INNER JOIN (select @curRowB := @curRowB + 1 AS row_number, 
            customer_id, 
            date from orders 
    ORDER BY customer_id asc, date desc offset 1) B
ON A.row_number = B.row_number AND A.customer_id = B.customer_id

正如你所看到的,我有一个子查询从orders表中选择所有记录,但是将row_number添加到A中,然后我做同样的事情,但只是向下移动一行(偏移1)并创建B.然后我就加入A和B,你得到的结果。

A
    row_id, customer, date
    1, 1, d1
    2, 1, d2
    3, 1, d3
    4, 2, d4
    5, 2, d5

B
    row_id, customer, date
    1, 1, d2
    2, 1, d3
    3, 2, d4
    4, 2, d5

加入A和B时

result
customer, date1, date2
1, d1, d2
1, d2, d3
2, d4, d5

更新:根据Marc的评论,尝试从相关查询(按客户)获取date2可能会更好,您可以选择小于date的日期并将结果限制为1行。

答案 1 :(得分:0)

你可以尝试这个,经过充分测试......

SELECT distinct(c1.customer_id) as 'customer_id', 

(select min(c2.order_date) from tmp2 c2 where c2.customer_id=c1.customer_id) as date1, (select max(c2.order_date) from tmp2 c2 where c2.customer_id=c1.customer_id) as date2, (DATEDIFF( (select max(c40.order_date) from tmp2 c40 where c40.customer_id=c1.customer_id) , (select min(c40.order_date) from tmp2 c40 where c40.customer_id=c1.customer_id) )) as 'days' FROM {TMP2 {1}}