过滤与订单状态不匹配的订单以及订单历史记录中的最后订单状态

时间:2016-08-23 11:02:03

标签: mysql sql prestashop prestashop-1.6

我正在使用Prestashop,我需要验证数据完整性,将每个订单的当前状态与注册的最后订单历史状态进行比较。

订单表:

╔══════════╦═══════════════╦
║ id_order ║ current_state ║
╠══════════╬═══════════════╬
║    1     ║       3       ║ 
║    2     ║       1       ║
║    3     ║       2       ║
║    4     ║       1       ║
╚══════════╩═══════════════╩

order_history 表:

╔══════════════════╦══════════╦════════════════╦═════════════════════╦
║ id_order_history ║ id_order ║ id_order_state ║      date_add       ║
╠══════════════════╬══════════╬════════════════╬═════════════════════╬
║        1         ║     1    ║       1        ║ 2016-08-01 11:00:00 ║
║        2         ║     2    ║       1        ║ 2016-08-02 12:00:00 ║
║        3         ║     1    ║       3        ║ 2016-08-03 13:00:00 ║
║        4         ║     3    ║       1        ║ 2016-08-04 14:00:00 ║
║        5         ║     3    ║       2        ║ 2016-08-05 15:00:00 ║
║        6         ║     2    ║       3        ║ 2016-08-06 16:00:00 ║
║        7         ║     4    ║       1        ║ 2016-08-07 17:00:00 ║
╚══════════════════╩══════════╩════════════════╩═════════════════════╩

(自愿遗漏表是为订单状态命名:1 =“付费”,2 =“已确认”,3 =“已发货”......)

通常,current_state顺序必须等于最后一个状态历史记录条目,但有时候不是(在我的示例中,对于#2的顺序,current_state = 1但是上次历史记录更新的id_order_state是2),这就是我想要的透露。

我这样做是为了获得每个最后的订单状态更新:

SELECT o.id_order, o.current_state, h.id_order_state, max(h.date_add)
FROM orders o, order_history h
WHERE o.id_order = h.id_order
GROUP BY o.id_order, o.current_state, h.id_order_state
ORDER BY o.id_order ASC

但这还不够,因为首先我只想要每个订单上次更新的状态:

╔══════════╦═══════════════╦════════════════╦═════════════════════╗
║ id_order ║ current_state ║ id_order_state ║   max(h.date_add)   ║
╠══════════╬═══════════════╬════════════════╬═════════════════════╣
║    1     ║       3       ║       3        ║ 2016-08-03 13:00:00 ║
║    2     ║       1       ║       3        ║ 2016-08-06 16:00:00 ║
║    3     ║       2       ║       2        ║ 2016-08-05 15:00:00 ║
║    4     ║       1       ║       1        ║ 2016-08-07 17:00:00 ║
╚══════════╩═══════════════╩════════════════╩═════════════════════╝

然后添加一个带WHERE current_state <> id_order_state的过滤器,只显示损坏的数据(如#2顺序):

╔══════════╦═══════════════╦════════════════╦═════════════════════╗
║ id_order ║ current_state ║ id_order_state ║   max(h.date_add)   ║
╠══════════╬═══════════════╬════════════════╬═════════════════════╣
║    2     ║       1       ║       3        ║ 2016-08-06 16:00:00 ║
╚══════════╩═══════════════╩════════════════╩═════════════════════╝

完整的SQL请求是否可以执行此操作?

4 个答案:

答案 0 :(得分:1)

E.g:

SELECT o.id_order
     , o.current_state
     , x.id_order_state
     , x.date_add
  FROM order_history x 
  JOIN 
     ( SELECT id_order,MAX(date_add) date_add FROM order_history GROUP BY id_order ) y 
    ON y.id_order = x.id_order 
   AND y.date_add = x.date_add
  JOIN orders o
    ON o.id_order = x.id_order
 WHERE x.id_order_state <> o.current_state;

答案 1 :(得分:0)

首先使用您已有的max(h.date_add)子句将订单历史记录选择到临时表中。

然后,您可以使用查询WHERE o.id_order = h.id_order

加入此项

首先将它放入临时表(或子查询)中,您已经消除了多行的问题。然后,您可以对订单状态进行比较,这将告诉您哪些订单状态不同步。

将其视为首先从历史记录表中预先过滤您不需要的数据。

答案 2 :(得分:0)

Klemart3D这不是一个基本的SQL查询:)抱歉,我没有看到标题是由另一个用户编辑的:),但是我猜(我希望XD)那是满足您需求的查询:

SELECT 
    o.`id_order`, o.`current_state`, h.`id_order_state`, h.`date_add`
FROM 
    `ps_order_history` h INNER JOIN (SELECT `id_order`, MAX(`date_add`) AS maxdateadd FROM `ps_order_history` GROUP BY id_order) laststatus ON (h.`id_order` = laststatus.`id_order` AND h.`date_add` = laststatus.`maxdateadd`)
    INNER JOIN `ps_orders` o ON (o.`id_order` = h.`id_order` AND o.`current_state` <> h.`id_order_state`)
ORDER BY h.`id_order` ASC

我已将标准前缀ps添加到表名。

答案 3 :(得分:0)

我发现了这种解决方法,但此处发布的其他一些查询比我的更好:

SELECT o.id_order, o.current_state, h.id_order_state, h.date
FROM ps_orders o
LEFT JOIN (
    SELECT id_order, id_order_state, max(date_add) as date
    FROM ps_order_history
    GROUP BY id_order
    ORDER BY id_order DESC
) h ON o.id_order = h.id_order
WHERE o.current_state <> h.id_order_state 
GROUP BY o.id_order
ORDER BY o.id_order DESC

我已将标准前缀ps添加到表名。

注意:此查询不适用于Prestashop的请求SQL(子查询生成&#34;未定义checkedFrom错误&#34; PS v1.6.0.14)。