Magento 在其数据库系统中使用 EAV 结构。我有这个查询,它为我提供了我的 magento 商店中产品的 product_id 和名称。
SELECT e.entity_id AS product_id, var.value AS product_name
FROM catalog_product_entity e, eav_attribute eav, catalog_product_entity_varchar var
WHERE
e.entity_type_id = eav.entity_type_id
AND eav.attribute_code = 'name'
AND eav.attribute_id = var.attribute_id
AND var.entity_id = e.entity_id
我需要帮助获取 product_url|price|image_url|description|manufacturer
最佳答案
我不打算发布整个 SQL 查询,因为尝试通过数据库手动从 Magento 中获取数据太乏味了,但我会说你走在正确的轨道上。为了减少这类事情的连接数量,我从 eav 表中检索我的 attribute_ids 并直接使用它们。这意味着我的查询将仅适用于 我 安装的 Magento,但这对我来说不是问题。
select attribute_code, attribute_id, backend_type from eav_attribute
where entity_type_id = (select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product')
and attribute_code in ('name', 'url_path', 'price', 'image', 'description', 'manufacturer');
产量:
+----------------+--------------+--------------+
| attribute_code | attribute_id | backend_type |
+----------------+--------------+--------------+
| description | 61 | text |
| image | 74 | varchar |
| manufacturer | 70 | int |
| name | 60 | varchar |
| price | 64 | decimal |
| url_path | 87 | varchar |
+----------------+--------------+--------------+
现在您已经准备好迎接单调乏味的生活了!对于每个属性代码,在您给定的属性 ID 上加入后端表 (catalog_product_entity_$BACKEND_TYPE
)。对我来说,这会将 sku/name/id 查询(您的查询实际上不需要加入产品,因为您使用 entity_id 来进行加入...)变成:
select p.sku, p.entity_id, n.value name
from catalog_product_entity p
join catalog_product_entity_varchar n on n.entity_id = p.entity_id
where n.attribute_id = 60;
继续添加新的连接语句|where-clause|select-clause 集,直到您拥有最初想要的所有连接。
也就是说,Jonathan 是正确的,使用 Magento 框架管理这些数据比通过数据库手动管理要容易得多。除非您需要一次加载大量产品(请注意,这里有两个假设,您可以努力减少其中一个),否则使用该框架会更加稳健。
希望对您有所帮助!
谢谢, 乔
关于php - 使用 SQL 查询从数据库导入 Magento 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5323102/