Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在购物车中仅显示包含产品的类别_Sql_Asp.net Mvc 3_Postgresql_Tree_Shopping Cart - Fatal编程技术网

Sql 如何在购物车中仅显示包含产品的类别

Sql 如何在购物车中仅显示包含产品的类别,sql,asp.net-mvc-3,postgresql,tree,shopping-cart,Sql,Asp.net Mvc 3,Postgresql,Tree,Shopping Cart,在购物车中,产品类别树以嵌套的集合形式定义为 create table artomlii ( artomaliik serial primary key, treeparent integer references artomlii, -- parent or null if root node categoryname char(50) ); 树的最大深度为5 树中的产品定义为 create table artomadu ( artomaliik integer prim

在购物车中,产品类别树以嵌套的集合形式定义为

create table  artomlii (
  artomaliik serial primary key,
  treeparent integer references artomlii,  -- parent or null if root node
  categoryname char(50) );
树的最大深度为5

树中的产品定义为

create table artomadu (
  artomaliik integer primary key references artomlii not null,
  productid char(20) primary key not null
  )
在购物车主页中,使用查询显示根目录

select * from artomlii where treeparent is null
根据登录用户的不同,某些根类别可以为空,但不包含 任何子类别中的任何产品。为此,自定义筛选器应用于artomadu表

此查询还显示空的根类别。 如何修复此问题,以便仅显示在其任何子类别中至少有一个产品的根类别

可以用递归或者其他的方法吗

with recursive cte as (
    select a.artomaliik, a.categoryname, a.artomaliik as treeparent
    from artomlii as a
    where a.treeparent is null

    union all

    select c.artomaliik, c.categoryname, a.artomaliik as treeparent
    from artomlii as a
        inner join cte as c on c.treeparent = a.treeparent
)
select distinct
    c.artomaliik, c.categoryname
from cte as c
where
    exists (select * from artomadu as a where a.artomaliik = c.treeparent)