当产品属于子类别时,为每个类别选择产品计数

时间:2014-09-27 09:33:32

标签: sql postgresql recursive-query

这是产品的表结构

PROD_ID     CATEG_ID
1           2
2           21
3           211
4           5
5           51

这是类别

的表格结构
CATEG_ID    PARENT_CATEG_ID
2           NULL
5           NULL
21          2
211         21
51          5

为每个类别(包括嵌套类别)选择产品计数时遇到困难。 例如,类别2包含1个产品,类别21包含1个产品,类别211包含1个产品,因为类别21221各自为直接/类别2 /类别2的间接祖先有3个产品。所以我需要一个查询或者只是一种方法来得到这样的东西:

CATEG_ID    PARENT_CATEG_ID    PRODUCT_COUNT
2           NULL               3   (including product count for categories 21 and 221)
5           NULL               2   (including product count for category 51)
21          2                  2   (including product count for category 221)
211         21                 1   (no category ancestor, only product count for self)
51          5                  1   (no category ancestor, only product count for self) 

是否可以只使用SQL或者我需要添加一些PHP?

2 个答案:

答案 0 :(得分:2)

以下应该这样做:

with recursive cat as (
  select categ_id, 
         parent_categ_id,
         categ_id as root_category, 
         1 as level
  from categories
  where parent_categ_id is null
  union all
  select c.categ_id,
         c.parent_categ_id,
         p.root_category,
         p.level + 1
  from categories c
    join cat as p on p.categ_id = c.parent_categ_id
)
select c.categ_id, 
       p.prod_id,
       (select count(*) from cat c2 where c2.level >= c.level and c2.root_category = c.root_category) as cnt
from cat c
  left join products p on p.categ_id = c.categ_id
;

递归查询首先构建整个类别树。它返回每个类别的根类别以及特定根类别的子树内类别的嵌套级别。 CTE本身会返回:

categ_id | parent_categ_id | root_category | level
---------+-----------------+---------------+------
       2 |          (null) |             2 |     1
      21 |               2 |             2 |     2
     211 |              21 |             2 |     3
       5 |          (null) |             5 |     1
      51 |               5 |             5 |     2

然后将其用于连接产品表,并对同一根类别(count(p.prod_id) over (partition by c.root_category order by level desc)部分)中包含的产品进行运行总计。所以完整查询的结果是:

categ_id | prod_id | product_count
---------+---------+--------------
       2 |       1 |             3
      21 |       2 |             2
     211 |       3 |             1
       5 |       4 |             2
      51 |       5 |             1

SQLFiddle:http://sqlfiddle.com/#!15/d6261/15

答案 1 :(得分:0)

这里我们在递归查询的帮助下检查 c1.categ 是否具有子类别,该查询通过自身在树下构建树来获取所有子类别ID。在子类别下

select c1.categ_id,c1.parent_categ_id,count(prods.prod_id)
as product_count from categ c1
join prods on prods.categ_id=c1.categ_id or prods.categ_id
in( with recursive tree(id,parent_id)as 
(select categ_id,parent_categ_id from categ 
where categ_id=c1.categ_id  
union all
select cat.categ_id,cat.parent_categ_id from categ cat
join tree on tree.id=cat.parent_categ_id) select id from tree)
group by c1.categ_id,c1.parent_categ_id order by product_count

结果如下

+----------+-----------------+---------------+
| categ_id | parent_categ_id | product_count |
+----------+-----------------+---------------+
|       51 |               5 |             1 |
|      211 |              21 |             1 |
|        5 |            NULL |             2 |
|       21 |               2 |             2 |
|        2 |            NULL |             3 |
+----------+-----------------+---------------+