SELECT AA.ORDER_NO,AA.OPERATING_TIME,,AA.NEW_STATUS
FROM
(
SELECT R.ORDER_NO,R.OPERATING_TIME,R.NEW_STATUS,R.RANK
FROM
(
SELECT A.ORDER_NO,A.OPERATING_TIME,A.NEW_STATUS,
@rownum:=@rownum+1,
if(@pno=A.ORDER_NO,@rank:=@rank+1,@rank:=1) as rank,
@pno:=A.ORDER_NO
FROM
(
SELECT ORDER_NO,OPERATING_TIME,NEW_STATUS FROM ORDER_STATUS_CHANGE_LOG a
WHERE OPERATING_TIME >= STR_TO_DATE('20160901','%Y%m%d')
ORDER BY ORDER_NO,OPERATING_TIME DESC
) A,
(select @rownum :=0 , @pno := null ,@rank:=0) B
) R
)AA
WHERE AA.RANK = 1;
mysql分组排序
最新推荐文章于 2024-06-21 18:07:31 发布
本文介绍了一种使用MySQL数据库查询每个订单的最新状态的方法。通过复杂的嵌套查询结构,确保了每个订单只返回最后一次状态变更记录,并且该记录的时间戳不早于指定日期。此查询适用于需要跟踪大量订单状态更新的应用场景。
摘要由CSDN通过智能技术生成