微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获取PostgreSQL中的所有类别和子类别?

如何解决如何获取PostgreSQL中的所有类别和子类别?

我正在使用 .net core 5、EF core 和 postgres 10 创建一个小型电子商务演示应用程序。
我正在尝试在我的主页上获取所有类别和子类别。

这是我的类别层次结构:

男士(父类别)

  • 上衣(子类别)
    • 正装衬衫(叶级类别)
    • T 恤(叶级类别)

女性

  • India & Fusion Wear

    • 库尔塔斯和西装
    • 纱丽
  • 鞋履

    • 平面
    • 高跟鞋

这是我的创建表和插入脚本:

CREATE TABLE category
(
    id                uuid      DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,parent_id         uuid                                             NULL,name              character varying(255)                           NOT NULL,short_description character varying(255)                           NOT NULL,activate_on       date                                             NULL,deactivate_on     date                                             NULL,url_segment       character varying(255)                           NOT NULL,Meta_keywords     character varying                                NOT NULL,Meta_description  character varying                                NOT NULL,sort_order        integer                                          NOT NULL,created_on        timestamp DEFAULT CURRENT_TIMESTAMP,created_by        character varying(100),modified_on       timestamp DEFAULT CURRENT_TIMESTAMP,modified_by       character varying(100),is_active         boolean
);



INSERT INTO public.category (id,parent_id,name,short_description,activate_on,deactivate_on,url_segment,Meta_keywords,Meta_description,sort_order,created_on,created_by,modified_on,modified_by,is_active)
VALUES ('0ca0c27c-f45b-4768-a3cb-958bfbf6df26','6dfdf5e4-673b-4134-b4a9-3a0b14b50e6a','T-Shirts','2021-02-21',NULL,'men-tshirts','t shirt for mens india,t shirt for men online,polo t shirts,sport t shirt,black t shirt,blue t shirt,white t shirt','Men''s T-shirts - Buy T-shirts for men online in India. Choose from a wide range of polo,round,V neck & more Men''s T-shirts in varIoUs designs at Myntra.',1,'2021-02-21 13:17:15.652506','glenn','',TRUE);
INSERT INTO public.category (id,is_active)
VALUES ('6927198d-9240-43b0-853a-0507cad3b529','Formal Shirts','men-formal-shirts','Men''s formal shirts,formal shirts for men,slim fit formal shirts for men,latest formal shirts for men,formal shirts for men online,formal shirts for men India','Formal Shirts for Men - Shop for trendy men''s formal shirts online in India at Myntra. ✯Free Shipping ✯Easy returns and exchanges ✯COD',2,'2021-02-21 13:21:16.125333',is_active)
VALUES ('0d3f9e43-12a2-4f8c-a008-a45cbf15fedd','Women','women','women online shopping,online shopping for women,women clothing,women dresses online','Online Shopping for Women. Shop Online from a wide range of womens clothing,shoes,Ladies bags & more in India @ Myntra ✯Free Shipping ✯COD ✯Easy returns and exchanges.','2021-02-21 13:26:04.447031',is_active)
VALUES ('6dfdf5e4-673b-4134-b4a9-3a0b14b50e6a','eb81c712-0643-45a2-b597-129a1f9892e2','Topwear','top-wear','mens topwear','2021-02-21 13:12:30.518814',is_active)
VALUES ('eb81c712-0643-45a2-b597-129a1f9892e2','Men','men','Men Topwear,Men Topwear in India,buy Men Topwear online in india,online store for Men Topwear,Shop online for Men Topwear,online shopping for Men Topwear,Online shopping for Men Topwear in India,reviews and prices','Men Topwear Online. Shop for Men Topwear in India ? Buy latest range of Men Topwear at Myntra ? Free Shipping ? COD ? Easy returns and exchanges','2021-02-21 13:10:29.204689',is_active)
VALUES ('731b01bd-fab7-475e-adb2-6322eb9eac68','0d3f9e43-12a2-4f8c-a008-a45cbf15fedd','India & Fusion Wear','fusion-wear','indo western fusion dresses,fusion kurtis,fusion saree,fusion dress,designer fusion wear,ethnic fusion wear,fusion wear online,indo western fusion,fusion outfits','Buy Latest Collection of Women Fusion Wear online in India. Choose From Kurtas,Designer Sarees,Dresses,Lehenga Choli & more of brands like Mitera,Anouk,Biba & more at Myntra Fashion store ✯ COD ✯ discounts','2021-02-21 13:32:52.616545',is_active)
VALUES ('3715c7fc-3c0e-4ee4-8fa0-dfd48e8e3a3a','731b01bd-fab7-475e-adb2-6322eb9eac68','Sarees','saree','saree,sarees,women saree,new saree collection,fancy saree,online saree shopping,saree blouse designs,latest saree,new saree design,latest saree design,sari online','Saree - Pick from the designer range of Saris online for ladies & girls. Buy formal,partywear,casual Sarees in varIoUs prints,patterns,types & more at Myntra store.','2021-02-21 13:35:26.166935',is_active)
VALUES ('34c0d8ef-19a0-4f3a-8439-6d0510cfba04','Kurtas & Suits','women-kurtas-kurtis-suits','kurtis online,designer kurti,ladies designers kurtis,designers kurtis,designer kurtis online,suits online','Women Kurtis & Kurtas Suits - Buy Designer Kurtas & Kurtis for Ladies online. Pick from Huge range kurtis,kurtas sets & suits for Women at Myntra ✯ COD ✯ Easy returns and exchanges','2021-02-21 13:36:59.067196',TRUE);

我想从我的数据库提取所有类别,以便像上面的类别层次结构一样在 UI 端轻松绑定。

有人可以帮我查询吗?

解决方法

---------- 修改了
Parent、Sub、Leaf的确定比较简单:

  1. Row Child id 为 null,然后为 Parent。
  2. Row child id 不为 null 并且 row 有子行然后是 Sub。
  3. 否则叶子。

唯一的困难是上面的2。
现在可以通过几种方式清除它:

  • 使用相同的基本递归 CTE,同时应用超前和滞后函数 在主查询中然后检查空值。但是对于层次结构,这会很快变得混乱。所以让我们拒绝这个想法。
  • 对 CTE 稍加修改,非递归选择是 父母。在递归选择中查看当前是否有子行 然后是子类别。

修改 CTE 并不是很糟糕,但是选择它作为父级仍然有些讨厌。因此,下面将其抽象为一个简单的 SQL 函数,该函数返回适当的类型(假设 id 是子代)。

create or replace 
function category_sub_or_leaf(id_in uuid)
  returns text
  language sql 
  strict 
as $$ 
    select case when exists (select null
                               from category
                               where parent_id = id_in 
                            )   
                then '(Sub Category)'
                else '(leaf level category)'
             end  ;
$$;

结果查询为:

with recursive heir (id,parent_id,name,short_description,sort_order,root_path,lev,cat_level) as
    ( select  id,(name)::text,1,'(Parent Category)'
        from category 
       where parent_id is null
      union all 
      select c.id,c.parent_id,c.name,c.short_description,c.sort_order,root_path || '>'||(c.name)::text,lev+1,category_sub_or_leaf(c.id) 
        from category c
        join heir     h 
          on c.parent_id = h.id
    ) --select * from heir;                                                              
select  (rpad(' ',4*(lev-1),' ') || short_description) || ' ' || cat_level   description
  from heir
 order by root_path,sort_order;

参见revised demo: Demo 还冲洗掉了女士足部用品,以在子类别中提供额外的层。即 2 层子类别。但是因为我不想打那么多字,所以我修改了表格,只需要必要的列就不需要任何东西了。

---------- 原文
您正在寻找的是 recursive cte。这里第一个(非递归项)选择获取没有定义 parent_id 的行。然后递归项(在联合之后)回顾前一行并检索子行。 (充其量是一个糟糕的描述。)查询构建返回第一个父级和当前深度的路径。然后使用这些生成的列来提供结果的顺序和缩进。见Demo

with recursive heir as 
     ( select  id,(name)::text path,1 lev
         from category 
        where parent_id is null
       union all 
       select c.id,path ||'>'||(c.name)::text,lev+1 
         from category c
         join heir     h 
           on c.parent_id = h.id
     ) 
select (rpad(' ',' ') || short_description) short_description
  from heir
 order by path,lev ;

注意:Demo 使用 Postgres 13 构建,因此函数 gen_random_uuid() 替换了 uuid_generate_v4()。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。