多个类别的递归sql查询

时间:2013-10-25 08:40:50

标签: mysql sql categories

我有一个如下所示的查询,它从两个表中提取类别数据,

SELECT pc.productCategoryId, pcs.parentCategoryId, pc.categoryId
FROM productCategory AS pc
LEFT JOIN productCategories AS pcs ON pcs.categoryId = pc.categoryId
WHERE pc.productId =110
ORDER BY pc.categoryId

这是结果

productCategoryId parentCategoryId categoryId Ascending
225               0                1
773               1                1283
773               132              1283
774               1283             10314

但这是理想的结果

productCategoryId parentCategoryId categoryId Ascending
    225               0                1
    773               1                1283
    774               1283             10314

我的目标是使用匹配的以下父类别ID保留记录,因此第三个应该以某种方式取出。  我的意思是每个以前的记录categoryid应该匹配下一个的parentcategoryId

0 个答案:

没有答案