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)