简体   繁体   中英

Is there any solution about this hierarchical structure of data model in SQL?

I have this data model. This data model is about e-shop structure by categories.

在此处输入图像描述

item_category - every subcategory on website. item - every item on e-shop (item_category_id - every subcategory on website (leaf)).

parent_id is self-referent and define the hierarchical structure. Roots of hierarchical structure mean main categories.

item_category_transitive_closure - it is a redundant data structure containing a transitive closure of the supercategory-subcategory relation, which is represented by the self-reference parent binding above the item_category table

  • Every representation of a link between two categories is meaningful
  • lies on the path to the root of the categories tree from the given category (#sub_cat_id) 0, M: 0, M
  • which are descendants of the category (#super_cat_id) within the category tree (0,M: 0,M)

Target is "Count of items by main categories" in SQL.

Thank you for your help

if you look at this link Get Root Node it should show you how to get the root (main) category for any category ID in your item_category hierarchy table. Just join this result set to whatever query you write to be able to include the Main Category in it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM