-1

我正在准备获取在网站上显示的产品 sku 以及该产品的类别。

输出:

+-------------+------------+
| sku         | category_n |
+-------------+------------+
|      855202 |      test1 |
|     87972_k |      test2 |
|      887997 |      test1 |
+-------------+------------+

我看过这些表:

catalog_category_product
catalog_product_entity
catalog_category_entity
catalog_category_entity_varchar

该查询只为一个 sku 返回了很多行,我得到了大约 100 条记录。我看不出哪一个是当前活跃的正确类别..

SELECT
    * 
FROM
    "catalog_category_product" as ccp 
JOIN
    "catalog_product_entity" as cpe ON "cpe.entity_id" = "ccp.product_id"
JOIN  
    "catalog_category_entity" as cat ON "cat.entity_id" = "ccp.category_id"
JOIN 
    "catalog_category_entity_varchar" as cv on cat.entity_id = cv."entity_id"
4

2 回答 2

0

假设您使用的是 mysql 数据库尝试不要在对象名称(表和列名称)周围使用引号

SELECT * 
FROM catalog_category_product as ccp 
JOIN catalog_product_entity as cpe ON cpe.entity_id = ccp.product_id
JOIN catalog_category_entity as cat ON cat.entity_id = ccp.product_id
JOIN catalog_category_entity_varchar as cv on cat.entity_id = cv.entity_id
于 2018-11-26T10:09:50.447 回答
0

您可以尝试使用左连接

SELECT
    * 
FROM
    "catalog_category_product" as ccp 
Left JOIN
    "catalog_product_entity" as cpe ON "cpe.entity_id" = "ccp.product_id"
Left JOIN  
    "catalog_category_entity" as cat ON "cat.entity_id" = "ccp.category_id"
Left JOIN 
    "catalog_category_entity_varchar" as cv on cat.entity_id = cv."entity_id"
于 2018-11-26T10:29:25.787 回答