mysql窗口函数

mccptt67  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(350)

订单在最终交付给客户之前具有多个订单状态。有时客户从不同的来源下多个订单。我们接受一个订单,但拒绝另一个订单。我想知道取消重复订单时,接受订单的最新订单状态是什么。

LEFT JOIN(
            SELECT order_id AS order_id,order_status,max(addtime(`timestamp`,'05:30:00')) AS timestamp
            FROM pe2.order_history
            GROUP BY 1,2
) oh ON oh.order_id = ord.order_id AND oh.timestamp <= c.time. 
c.time. is duplicate order cancellation time.
mfpqipee

mfpqipee1#

我们得到所需的客户订单,然后得到同一订单的最新状态,即取消重复订单之前的状态:

SELECT order.*,
(SELECT order_status FROM order_history AS oh 
WHERE order_id = order.id AND timestamp <= cancel_time
ORDER BY timestamp DESC LIMIT 1) AS last_status
FROM order
WHERE order.ID = accepted_order

相关问题