通过数据库查询(EAV)获取产品报告
Getting report of products through database query (EAV)
我正在尝试了解有关 EAV 的更多信息,并且我正在对 magento 2 示例数据数据库进行一些测试。
我只想检索产品 ID 和产品描述,但与 catalog_product_entity
table 和 catalog_product_entity_text
中的产品总数不匹配table:
catalog_product_entity
table中有 2046 件商品:
如果我使用以下查询,我会得到 2052 个结果:
SELECT product.entity_id as "Description",description.value FROM catalog_product_entity_text description,catalog_product_entity product where product.entity_id = description.entity_id ORDER BY product.entity_id
我假设在某些情况下 entity_id 匹配文本 table 中的不止一行。或许可以有除描述以外的文本属性?
试试这个:
SELECT product.entity_id as Product_id,
COUNT(*) AS count,
GROUP_CONCAT(description.value) AS Description
FROM catalog_product_entity_text description
LEFT OUTER JOIN catalog_product_entity product ON product.entity_id = description.entity_id
GROUP BY product.entity_id
ORDER BY product.entity_id
我不熟悉 Magento 的 EAV tables,但我认为 table 应该有一个属性标识符列和一个 entity_id。如果您只需要描述而不需要其他文本属性,则可能必须根据属性类型进行过滤。
P.S.: 我调整了您的查询以使用现代 JOIN
语法。您不应该使用 "comma-style" 联接,它们在 1992 年就过时了。
我正在尝试了解有关 EAV 的更多信息,并且我正在对 magento 2 示例数据数据库进行一些测试。
我只想检索产品 ID 和产品描述,但与 catalog_product_entity
table 和 catalog_product_entity_text
中的产品总数不匹配table:
catalog_product_entity
table中有 2046 件商品:
如果我使用以下查询,我会得到 2052 个结果:
SELECT product.entity_id as "Description",description.value FROM catalog_product_entity_text description,catalog_product_entity product where product.entity_id = description.entity_id ORDER BY product.entity_id
我假设在某些情况下 entity_id 匹配文本 table 中的不止一行。或许可以有除描述以外的文本属性?
试试这个:
SELECT product.entity_id as Product_id,
COUNT(*) AS count,
GROUP_CONCAT(description.value) AS Description
FROM catalog_product_entity_text description
LEFT OUTER JOIN catalog_product_entity product ON product.entity_id = description.entity_id
GROUP BY product.entity_id
ORDER BY product.entity_id
我不熟悉 Magento 的 EAV tables,但我认为 table 应该有一个属性标识符列和一个 entity_id。如果您只需要描述而不需要其他文本属性,则可能必须根据属性类型进行过滤。
P.S.: 我调整了您的查询以使用现代 JOIN
语法。您不应该使用 "comma-style" 联接,它们在 1992 年就过时了。