MySQL 相邻两条数据相减

<!-- 计算每两次消费的间隔天数 -->
SELECT B.MEN_ID,TIMESTAMPDIFF(DAY,B.PRE_DATE,B.CURR_DATE) AS DAYS FROM 
(
    <!-- 行变列 -->
    SELECT A.MEN_ID,A.ORDER_DATE AS CURR_DATE,@A.ORDER_DATE AS PRE_DATE,@A.ORDER_DATE:= A.ORDER_DATE
    FROM TBL_MAS_ORDER_MASTER A,(SELECT @A.ORDER_DATE:=NULL) S
) B WHERE B.PRE_DATE IS NOT NULL

 

posted @ 2018-04-19 19:03  Bob.Xie  阅读(4844)  评论(0编辑  收藏  举报