【问题标题】:Magento 1.x. Get either simple or configuable product by sqlMagento 1.x。通过 sql 获取简单或可配置的产品
【发布时间】:2018-01-31 09:06:16
【问题描述】:

为了对 magento 中的销售数据进行市场购物篮分析,我需要从 magento 检索每张发票中包含的产品。到目前为止,这就是我得到的。

SELECT 
    tblInvoice.increment_id AS orderId,
    tblLine.product_id AS productId,
    tblLine.sku as productSku,
    tblLine.qty as qty
FROM 
    sales_flat_invoice AS tblInvoice
RIGHT JOIN 
    sales_flat_invoice_item AS tblLine 
        ON tblInvoice.entity_id = tblLine.parent_id

但是,问题在于,对于同时包含可配置产品和简单产品的产品,我得到了两者。如果它是单独的,我只需要简单的产品。

 orderId    productId   productSku  qty     
100000004   456         mpd00338    1.0000 <-- The simple product in this pair should be removed
100000004   476         mpd00338    1.0000 <-- The simple product in this pair should be removed
100000006   374         abl004      4.0000 <-- This simple product is alone, and should therefore stay
100000006   417         wbk002      1.0000 <-- Once again the simple product in this pair should be removed
100000006   284         wbk002      1.0000 <-- Once again the simple product in this pair should be removed

我最初的想法是使用可见性来删除冗余实体​​,但我不确定这是否适用于所有情况,或者是否可能存在两者都可见或隐藏的情况?

所以我的问题是

  1. 我应该如何更改我的 sql 语句以便在可配置产品的情况下仅选择一种产品(而不删除那些独立的简单产品?

  2. 此解决方案是否适用于 1.7、1.8 和 1.9 版本?

  3. 是否存在解决方案不起作用的情况?

注意。对于分组产品和捆绑产品,我当然应该得到所有简单的产品,因为它们实际上代表了一个真实的产品。

【问题讨论】:

    标签: php sql magento magento-1.9 business-intelligence


    【解决方案1】:

    这是您需要使用的查询来排除那些对应的可配置产品已包含在列表中的简单产品

    只需将以下部分附加到您的原始查询中

    JOIN
     sales_flat_order_item as sfoi
         ON sfoi.item_id = tblLine.order_item_id 
    WHERE sfoi.parent_item_id IS NULL 
    

    所以,您的完整查询应该是这样的

    SELECT 
            tblInvoice.increment_id AS orderId,
            tblLine.product_id AS productId,
            tblLine.sku as productSku,
            tblLine.qty as qty
        FROM 
            sales_flat_invoice AS tblInvoice
        RIGHT JOIN 
            sales_flat_invoice_item AS tblLine 
                ON tblInvoice.entity_id = tblLine.parent_id
        JOIN
         sales_flat_order_item as sfoi
             ON sfoi.item_id = tblLine.order_item_id 
        WHERE sfoi.parent_item_id IS NULL
    

    sales_flat_order_item 数据库表有这个 parent_item_id 列,表示它是某个父产品(通常是可配置产品)的子产品。表 sales_flat_order_item 中 parent_item_id 为空的所有记录都是父产品或单个产品(简单/分组/捆绑)。

    此 MySQL 查询适用于所有版本和所有场景。

    【讨论】:

    • 非常感谢。我最终使用的解决方案是一个 hack,它也完成了这项工作,但这似乎是正确的做法。
    【解决方案2】:

    我找到的解决方案是将数据与 catalog_product_entity 连接,然后按 catalog_product_entity.type_id 对数据进行排序。这样所有简单的产品都在底部,当使用 GROUP BY 时,我可以确保唯一的单独的时候选择简单的产品。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多