【问题标题】:How to pull all the product id, skus, product names, description in magento using only mysql?如何仅使用 mysql 在 magento 中提取所有产品 id、skus、产品名称、描述?
【发布时间】:2014-04-28 09:46:49
【问题描述】:

我将如何使用 mysql 从 Magento 数据库中提取所有产品 ir、skus、产品名称(标题)和 desxription?我使用以下查询并获得了除产品名称之外的所有属性。

SELECT e.entity_id, e.sku, eav.value AS 'description'
FROM catalog_product_entity e
JOIN catalog_product_entity_text eav
  ON e.entity_id = eav.entity_id
JOIN eav_attribute ea
  ON eav.attribute_id = ea.attribute_id
WHERE ea.attribute_code = 'description'

【问题讨论】:

  • 考虑提供适当的 DDL(和/或 sqlfiddle)以及所需的结果集。
  • 谢谢。但是你能在这里写完整的代码吗?哪个表包含产品名称?
  • @user3580780。我不认为你这样做的方式是可靠的。 eav_attribute 表中有许多属性,不同实体的代码相同(descriptionname 用于类别和产品)。如果你这样做,你可能会得到错误的连接条件,你不会得到任何结果。

标签: mysql magento


【解决方案1】:

标题可以从一个商店视图到另一个不同。描述也是如此。此外,一些商店视图可以使用后端设置的默认值。

这是关于如何获取特定商店视图 (id 1) 的所有产品所需数据(sku、名称、描述)的完整查询。

SELECT 
    `e`.`sku`, 
    IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS `name`,
    IF(at_description.value_id > 0, at_description.value, at_description_default.value) AS `description`

FROM 
   `catalog_product_entity` AS `e` 
    INNER JOIN 
         `catalog_product_entity_varchar` AS `at_name_default` 
               ON (`at_name_default`.`entity_id` = `e`.`entity_id`) AND 
                  (`at_name_default`.`attribute_id` = (SELECT attribute_id FROM `eav_attribute` ea LEFT JOIN `eav_entity_type` et ON ea.entity_type_id = et.entity_type_id  WHERE `ea`.`attribute_code` = 'name' AND et.entity_type_code = 'catalog_product')) AND 
                  `at_name_default`.`store_id` = 0 
    LEFT JOIN 
          `catalog_product_entity_varchar` AS `at_name` 
               ON (`at_name`.`entity_id` = `e`.`entity_id`) AND 
                  (`at_name`.`attribute_id` = (SELECT attribute_id FROM `eav_attribute` ea LEFT JOIN `eav_entity_type` et ON ea.entity_type_id = et.entity_type_id  WHERE `ea`.`attribute_code` = 'name' AND et.entity_type_code = 'catalog_product')) AND 
                  (`at_name`.`store_id` = 1) 
    INNER JOIN 
         `catalog_product_entity_text` AS `at_description_default` 
               ON (`at_description_default`.`entity_id` = `e`.`entity_id`) AND 
                  (`at_description_default`.`attribute_id` = (SELECT attribute_id FROM `eav_attribute` ea LEFT JOIN `eav_entity_type` et ON ea.entity_type_id = et.entity_type_id  WHERE `ea`.`attribute_code` = 'description' AND et.entity_type_code = 'catalog_product')) AND 
                  `at_description_default`.`store_id` = 0 
    LEFT JOIN 
          `catalog_product_entity_text` AS `at_description` 
               ON (`at_description`.`entity_id` = `e`.`entity_id`) AND 
                  (`at_description`.`attribute_id` = (SELECT attribute_id FROM `eav_attribute` ea LEFT JOIN `eav_entity_type` et ON ea.entity_type_id = et.entity_type_id  WHERE `ea`.`attribute_code` = 'description' AND et.entity_type_code = 'catalog_product')) AND 
                  (`at_description`.`store_id` = 1) 

如果您希望它用于其他商店视图,只需在以下几行中将值 1 替换为您想要的 id

(`at_name`.`store_id` = 1) 

(`at_description`.`store_id` = 1)

我不知道你为什么需要这个 sql 格式。这是一个奇怪且很大的错误源。可以通过代码轻松搞定:

$collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect(array('sku', 'name', 'description'));
foreach ($collection as $item) {
    $sku = $item->getSku();
    $name = $item->getName();
    $description = $item->getDescription(); 
    //do something with $sku, $name & $description
}

【讨论】:

  • 非常感谢您的回复。我知道mysql,所以试图找到一种使用mysql生成sku、title和description的方法。
  • @user3580780。嗯......上面的查询应该可以解决问题。我在 Magento 的示例数据库上对其进行了测试,并且可以正常工作。
  • @user3580780。它有效,请接受答案。不要留下松散的结局。
  • 我如何在这里接受答案?是否有任何类型的“是/否”按钮,以便显示我接受了它?
  • @user3580780 在任何答案的投票计数下都应该有一个复选标记。点击它。在此处阅读更多信息:stackoverflow.com/tourAsk questions, get answers, no distractions 部分
【解决方案2】:

这是另一个显示entity_id, product_name, sku的查询

SELECT
    catalog_product_entity_varchar.entity_id,
    catalog_product_entity_varchar.`value` AS product_name,
    catalog_product_entity.sku
FROM
    catalog_product_entity_varchar
INNER JOIN catalog_product_entity ON catalog_product_entity_varchar.entity_id = catalog_product_entity.entity_id
WHERE
    catalog_product_entity_varchar.entity_type_id = (
        SELECT
            entity_type_id
        FROM
            eav_entity_type
        WHERE
            entity_type_code = 'catalog_product'
    )
AND attribute_id = (
    SELECT
        attribute_id
    FROM
        eav_attribute
    WHERE
        attribute_code = 'name'
    AND entity_type_id = (
        SELECT
            entity_type_id
        FROM
            eav_entity_type
        WHERE
            entity_type_code = 'catalog_product'
    )
)

【讨论】:

  • 如果您在全局值上为 name 设置了值,而在特定商店视图上设置了不同的值,则此方法不起作用。您的查询未考虑商店视图。我认为它会检索所有可用商店视图的所有值。
【解决方案3】:

要获取产品名称,请尝试

$sql = "SELECT `value`
FROM catalog_product_entity_varchar
WHERE entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product') 
AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'name' AND entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product'))";

$results = $readConnection->fetchAll($sql);

【讨论】:

    【解决方案4】:

    此查询包含产品nameimagepricequantitydescription

    SET @etype = (SELECT 
                        entity_type_id
                    FROM
                        eav_entity_type
                    WHERE
                        entity_type_code = 'catalog_product');
    

    产品name属性ID

    SET @name  = (SELECT 
                attribute_id
            FROM
                eav_attribute
            WHERE
                attribute_code = 'name'
                    AND entity_type_id = @etype);
    

    产品image属性ID

    SET @image  = (SELECT 
                attribute_id
            FROM
                eav_attribute
            WHERE
                attribute_code = 'image'
                    AND entity_type_id = @etype);                
    

    产品小图属性ID

    SET @smallimage = (SELECT 
            attribute_id
            FROM 
               eav_attribute
            WHERE 
               attribute_code = 'small_image'
                   AND entity_type_id = @etype);
    

    产品price属性ID

    SET @price  = (SELECT 
                attribute_id
            FROM
                eav_attribute
            WHERE
                attribute_code = 'price'
                    AND entity_type_id = @etype);
    

    产品description属性ID

    SET @description  = (SELECT 
                attribute_id
            FROM
                eav_attribute
            WHERE
                attribute_code = 'description'
                    AND entity_type_id = @etype);
    

    -- 管理存储 ID -- SET @store = 0;

    SELECT 
        e.entity_id AS 'id',
        e.sku,
        v1.value AS 'name',
        v2.value AS 'image',
        s2.value AS 'small_image',
        si.qty AS 'stock qty',
        d1.value AS 'price',
        s1.value AS 'description'
    FROM
        catalog_product_entity e
            LEFT JOIN
        cataloginventory_stock_item si ON e.entity_id = si.product_id
            LEFT JOIN
        catalog_product_entity_varchar v1 ON e.entity_id = v1.entity_id
            AND v1.store_id IN (0,1,2)
            AND v1.attribute_id = @name
            LEFT JOIN
        catalog_product_entity_varchar v2 ON e.entity_id = v2.entity_id
            AND v2.store_id IN (0,1,2)
            AND v2.attribute_id = @image 
            LEFT JOIN        
        catalog_product_entity_varchar s2 ON e.`entity_id` = s2.entity_id
        AND s2.store_id IN (0,1,2)
        AND s2.`attribute_id` = @smallimage
        LEFT JOIN
        catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
            AND d1.store_id IN (0,1,2)
            AND d1.attribute_id = @price
            LEFT JOIN
            catalog_product_entity_text s1 ON e.entity_id = s1.entity_id
           AND s1.store_id IN (0,1,2)
           AND s1.attribute_id = @description ;
    

    【讨论】:

      【解决方案5】:

      下面是简单的方法。

      select 
      sku.entity_id, sku.sku, #get sku and entity
      productName.value, #get name
      description.value #get description
      from 
      catalog_product_entity as sku,
      catalog_product_entity_varchar as productName,
      catalog_product_entity_text as description
      where
      productName.attribute_id = 73 
      and
      sku.entity_id = productName.entity_id
      and
      description.attribute_id = 75
      and
      sku.entity_id = description.entity_id;
      

      【讨论】:

      • 最好在编写答案时解释代码的作用,而不仅仅是发布代码。
      猜你喜欢
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 2014-06-29
      • 2018-12-10
      相关资源
      最近更新 更多