Php 面包屑的递归sql查询
我需要此数据库的快速递归sql查询:Php 面包屑的递归sql查询,php,mysql,Php,Mysql,我需要此数据库的快速递归sql查询: SITE CATEGORY DIR PARENT_CATEGORY domain1.com home home domain1.com kitchen kitchen home domain1.com appliances apps home domain1.com tables
SITE CATEGORY DIR PARENT_CATEGORY
domain1.com home home
domain1.com kitchen kitchen home
domain1.com appliances apps home
domain1.com tables tables kitchen
domain2.com home home-dir
domain2.com bathroom bath
domain2.com garden garden
domain3.com fun funny
我需要的是一个函数,它将为我提供从当前类别到顶级类别的父类别数组。例如,对于domain1.com表,它应该返回:
$breadcrumb = Array(
[0]=>Array(
'title'='home',
'dir'='home'
);
[1]=>Array(
'title'='kitchen',
'dir'='kitchen'
);
[2]=>Array(
'title'='tables',
'dir'='tables'
);
);
所以基本上,它应该返回所请求类别和站点的面包屑。
函数$category,$site{
返回数组。
}查询有效,将为您提供高达7级的层次结构深度,所有父级都在一行中。 有些类似: SITE CATEGORY level1 level2 level3 level4 level5 -------------------------------------------------------------------------------- domain1.com tables tables kitchen home NULL NULL
注意:您可能需要做一些调整,然后按层次顺序进行调整?你需要定义最大深度是的,我需要一个函数来查找所有定义了父级的。。。当parent=时,这就是它在顶部类别中的位置…不是真的。您使用的表结构与我发布的表结构不同,我确信可以有一个查询,它只需查询表即可获得所有级别,无论有多少级别。
select level1, level2, site, category, dir, parent_category
if(level7 is not null,7,
if(level6 is not null and level7 is null,6,
if(level5 is not null and level6 is null,5,
if(level4 is not null and level5 is null,4,
if(level3 is not null and level4 is null,3,
if(level2 is not null and level3 is null,2,1)))))) depth
from
(select
site,category,dir,parent_category
site level1,
parent_category level2,
(select parent_category from <table> i where one.parent_category=i.category) level3,
(select parent_category from <table> i where level3=i.category) level4,
(select parent_category from <table> i where level4=i.category) level5,
(select parent_category from <table> i where level5=i.category) level6,
(select parent_category from <table> i where level6=i.category) level7
from
<table> one
WHERE one.site = ?
)a;