Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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
Mysql 递归动态查询_Mysql_Sql_Hierarchical Data_Recursive Query_Mifos - Fatal编程技术网

Mysql 递归动态查询

Mysql 递归动态查询,mysql,sql,hierarchical-data,recursive-query,mifos,Mysql,Sql,Hierarchical Data,Recursive Query,Mifos,我有一个递归SQL查询,其中我获得了每个办公室(一个办公室属于更高级别的办公室等)的ID层次结构——灵感来自@leftclickben在以下内容中的回答: 简而言之,所有结果必须以1开头 一个很好的例子: 根据@Hart CO的建议,我用两个工会的三个问题解决了这个问题: (select @pv := o.office_id, o.display_name, ifnull(( select concat(concat(group_concat(@p

我有一个递归SQL查询,其中我获得了每个办公室(一个办公室属于更高级别的办公室等)的ID层次结构——灵感来自@leftclickben在以下内容中的回答:

简而言之,所有结果必须以1开头

一个很好的例子:


根据@Hart CO的建议,我用两个工会的三个问题解决了这个问题:

(select @pv := o.office_id, o.display_name, ifnull((
                        select concat(concat(group_concat(@pv := t.parent_office_id order by t.parent_office_id asc SEPARATOR '.' ), '.'), t.office_id)  pivot
                        from (select * from office order by parent_office_id desc) t 
                                                where t.office_id = @pv 
                                            ), '1.') 'hierarchy'
from office o
where office_id not in (26, 27, 28, 29, 30, 32, 33, 34, 41, 57, 58, 59, 60, 61, 62, 63, 64, 73, 74, 75, 76, 77, 79, 82, 91, 96, 101, 102, 103, 104)
group by o.office_id
order by o.parent_office_id desc)
 union
(

select @pv := o.office_id, o.display_name, (
                        select concat(concat(group_concat(@pv := t.parent_office_id order by t.parent_office_id asc SEPARATOR '.' ), '.'), t.office_id)  pivot
                        from (select * from office order by parent_office_id  DESC) t 
                                                where t.office_id = @pv 
                                            ) 'hierarchy'
from office o
where office_id in (26, 27, 28, 29, 30, 32, 33, 34, 41, 57, 58, 59, 60, 61, 62, 63, 64, 91, 96, 101, 102, 103, 104)
group by o.office_id
order by o.parent_office_id desc
)
union
(select @pv := o.office_id, o.display_name, (
                        select concat(concat(group_concat(@pv := t.parent_office_id order by (select parent_office_id from office where office_id = t.parent_office_id)  asc SEPARATOR '.' ), '.'), t.office_id)  pivot
                        from (select * from office oo order by (select parent_office_id from office where office_id = oo.parent_office_id) deSC) t 
                                                where t.office_id = @pv 
                                            ) 'hierarchy'
from office o
where office_id in (73, 74, 75, 76, 77, 79, 82)
group by o.office_id
 order by o.parent_office_id desc
);
事实上,第一个查询是直截了当的:parent_id更小

对于第二个查询,在底层的父\u id更大

对于第三个查询,父项的父项id更大,这就是为什么我选择了按块排序的子查询,用于
group\u concat
和别名为
t
的子查询


我终于可以继续我的ETL了。

如果过滤到层次结构列表以1开头的位置,是否有正确的结果?在这种情况下,它是一个向后的层次结构。所以,当我从1开始时,我应该得到1(实际结果为NULL),如果我从2开始,我应该得到1.2,依此类推……我的意思是,你的结果集除了包含坏的层次结构外,还包含所有正确的层次结构吗?如果是这样的话,你可以过滤掉坏掉的,然后处理掉。好吧,听起来是个不错的方法@Mohamedennahdielidris很抱歉,您的样本与您的小提琴不匹配,值97和101不存在
- for some office (ID = 97), its hierarchy is 1.2.4.14.97 (accurate value);

- for another case (ID = 101), I get: 111.101 (broken hierarchy);
(select @pv := o.office_id, o.display_name, ifnull((
                        select concat(concat(group_concat(@pv := t.parent_office_id order by t.parent_office_id asc SEPARATOR '.' ), '.'), t.office_id)  pivot
                        from (select * from office order by parent_office_id desc) t 
                                                where t.office_id = @pv 
                                            ), '1.') 'hierarchy'
from office o
where office_id not in (26, 27, 28, 29, 30, 32, 33, 34, 41, 57, 58, 59, 60, 61, 62, 63, 64, 73, 74, 75, 76, 77, 79, 82, 91, 96, 101, 102, 103, 104)
group by o.office_id
order by o.parent_office_id desc)
 union
(

select @pv := o.office_id, o.display_name, (
                        select concat(concat(group_concat(@pv := t.parent_office_id order by t.parent_office_id asc SEPARATOR '.' ), '.'), t.office_id)  pivot
                        from (select * from office order by parent_office_id  DESC) t 
                                                where t.office_id = @pv 
                                            ) 'hierarchy'
from office o
where office_id in (26, 27, 28, 29, 30, 32, 33, 34, 41, 57, 58, 59, 60, 61, 62, 63, 64, 91, 96, 101, 102, 103, 104)
group by o.office_id
order by o.parent_office_id desc
)
union
(select @pv := o.office_id, o.display_name, (
                        select concat(concat(group_concat(@pv := t.parent_office_id order by (select parent_office_id from office where office_id = t.parent_office_id)  asc SEPARATOR '.' ), '.'), t.office_id)  pivot
                        from (select * from office oo order by (select parent_office_id from office where office_id = oo.parent_office_id) deSC) t 
                                                where t.office_id = @pv 
                                            ) 'hierarchy'
from office o
where office_id in (73, 74, 75, 76, 77, 79, 82)
group by o.office_id
 order by o.parent_office_id desc
);