mysql - 获取状态为 "shipped"且状态为 "wire transfer"未付款的所有订单

标签 mysql magento-1.4

我正在直接使用 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 模型/集合来收集数据。

最佳答案

您正在查找具有“已发货”但没有“银行支付”的实体。您想要在不同行上查找这些值。换句话说,您希望接受具有 'shipped' 行的实体,但前提是不存在具有 'bank payment' 的行。

您可以在 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;

关于mysql - 获取状态为 "shipped"且状态为 "wire transfer"未付款的所有订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17817820/

相关文章:

php - 从 PHP (jQuery/AJAX) 插入 MySQL

php - 加快 php::PDO->__construct

magento - Magento 中产品详细信息页面上的自定义变量

Magento - 菜单显示类别和类别中的产品

python - SQLAlchemy 突然停止使用 MySQLdb

php - 使用我的数据库创建一个简单的数组

php - 使用 IN 查询在表列中查找值

magento - 将 Magento 从 1.4.1 升级到 1.9 的最佳方法

Magento 订单导入/导出

php - 使用常规登录密码保护 Magento Storeview