0

我正在直接使用 Magento DB,试图获取订单状态为 的所有订单的列表Shipped,不包括通过电汇支付的订单 ( bankpayment)。这是我当前的查询:

SELECT * FROM (
  SELECT 
    so.entity_id, 
    so.created_at, 
    MAX(soe.created_at) AS shipped_at 
  FROM sales_order so 
  LEFT OUTER JOIN sales_order_entity soe 
    ON (soe.parent_id=so.entity_id) 
  LEFT OUTER JOIN sales_order_entity_varchar soev 
    ON (soev.entity_id = soe.entity_id) 
    AND (soev.value <> 'bankpayment') 
  WHERE (soev.value = 'shipped') 
  GROUP BY so.entity_id) AS shipped 
WHERE (shipped.shipped_at >= '2013-05-01 04:00:00') 
AND (shipped.shipped_at <= '2013-05-02 03:59:59');

我不是一个真正的 SQL 专家,你可以看到我在哪里尝试选择未通过电汇支付的订单,使用此行:

AND (soev.value <> 'bankpayment')

但它没有按预期工作,我怀疑它与WHERE子句有关:

WHERE (soev.value = 'shipped')

我不知道如何正确编写此查询。有什么建议吗?

注意- 在这种情况下,我无法使用 Magento 模型/集合来收集数据。

4

1 回答 1

0

您正在寻找具有'shipped' 但没有'bankpayment'. 您想在不同的行上查找这些值。换句话说,您希望接受具有'shipped' 的行的实体,但仅当没有具有 的行时'bankpayment'

having您可以在子句中执行此逻辑。好吧,你没有一个having条款。您有一个带有where外部子句的子查询。尽管这是等效的,但我正在简化查询以消除子查询。然后,我使用该having子句检查两个值的日期和存在/不存在:

SELECT so.entity_id, so.created_at, 
       MAX(soe.created_at) AS shipped_at 
FROM sales_order so LEFT OUTER JOIN
     sales_order_entity soe 
     ON soe.parent_id = so.entity_id LEFT OUTER JOIN
     sales_order_entity_varchar soev 
     ON soev.entity_id = soe.entity_id
GROUP BY so.entity_id
having MAX(soe.created_at) >= '2013-05-01 04:00:00' AND 
       MAX(soe.created_at) <= '2013-05-02 03:59:59' and
       SUM(soev.value = 'shipped') > 0 and
       sum(soev.value = 'bankpayment') = 0;
于 2013-07-23T18:22:13.127 回答