通过“电汇”未支付的“已发货”状态获取所有订单

时间:2013-07-23 18:10:32

标签: mysql magento-1.4

我直接与Magento数据库合作,尝试获取订单状态为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模型/集合来收集数据。

1 个答案:

答案 0 :(得分: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;