【问题标题】:MariaDb query for max depth in graphMariaDb 查询图中的最大深度
【发布时间】:2019-03-31 15:19:31
【问题描述】:

我有一个递归表来表示监督关系,一个人可以监督很多人并受到其他人的监督。

这里是一个示例数据:

+--------+------------+ |人 |监督 | +--------+------------+ | 1 | 2 | | 1 | 3 | | 1 | 4 | | 3 | 5 | | 4 | 5 | | 5 | 8 | | 4 | 6 | | 4 | 7 | | 6 | 9 | | 7 | 9 | | 9 | 10 | | 10 | 11 | | 3 | 11 | +--------+------------+

可以表示为: 其中,彩色节点代表深度最大的路径。

我的问题是,创建一个 MariaDb(10.4.1)/Mysql 查询,它返回一个包含路径的行,在这种情况下返回包含键的行,例如:1、4、6、9、10、11 或 1, 4、7、9、10、11

选择.... +------+ |路径 | +------+ | 1 | | 4 | | 6 | | 9 | | 10 | | 11 | +------+

【问题讨论】:

  • 你的 MySQL 版本是多少?
  • 哎呀...它的maria db 10.4.1,我认为它与Mysql 8+兼容

标签: mysql graph path mariadb recursive-query


【解决方案1】:

这个会返回所有可能的路径和深度:

with recursive rcte as (
  select person, supervises,
    concat(person, ',', supervises) as path,
    1 as depth
  from graph
  union all
  select r.person, g.supervises,
    concat(r.path, ',', g.supervises),
    r.depth + 1
  from graph g
  join rcte r on r.supervises = g.person
)
select *
from rcte
order by person, supervises

结果:

person | supervises | path          | depth
-------|------------|---------------|------
     1 |          2 | 1,2           | 1
     1 |          3 | 1,3           | 1
     1 |          4 | 1,4           | 1
     1 |          5 | 1,3,5         | 2
     1 |          5 | 1,4,5         | 2
     1 |          6 | 1,4,6         | 2
     1 |          7 | 1,4,7         | 2
     1 |          8 | 1,4,5,8       | 3
     1 |          8 | 1,3,5,8       | 3
     1 |          9 | 1,4,6,9       | 3
     1 |          9 | 1,4,7,9       | 3
     1 |         10 | 1,4,7,9,10    | 4
     1 |         10 | 1,4,6,9,10    | 4
     1 |         11 | 1,4,6,9,10,11 | 5
     1 |         11 | 1,3,11        | 2
     1 |         11 | 1,4,7,9,10,11 | 5
     3 |          5 | 3,5           | 1
     3 |          8 | 3,5,8         | 2
     3 |         11 | 3,11          | 1
     4 |          5 | 4,5           | 1
     4 |          6 | 4,6           | 1
     4 |          7 | 4,7           | 1
     4 |          8 | 4,5,8         | 2
     4 |          9 | 4,6,9         | 2
     4 |          9 | 4,7,9         | 2
     4 |         10 | 4,6,9,10      | 3
     4 |         10 | 4,7,9,10      | 3
     4 |         11 | 4,6,9,10,11   | 4
     4 |         11 | 4,7,9,10,11   | 4
     5 |          8 | 5,8           | 1
     6 |          9 | 6,9           | 1
     6 |         10 | 6,9,10        | 2
     6 |         11 | 6,9,10,11     | 3
     7 |          9 | 7,9           | 1
     7 |         10 | 7,9,10        | 2
     7 |         11 | 7,9,10,11     | 3
     9 |         10 | 9,10          | 1
     9 |         11 | 9,10,11       | 2
    10 |         11 | 10,11         | 1

Demo

要获得最大深度,只需使用

select max(depth) from rcte

【讨论】:

  • 非常好!!!非常感谢!如果我想在 SQL 中添加一个“where”子句?开始我的搜索,比如where person=4 然后检索 '4, 6, 9, 10, 11' ?
  • @JoãoCota 您可以在 CTE 的第一个 UNION 部分执行此操作:db-fiddle.com/f/cHfuxsBfzpoyqfspn4kXTo/0
猜你喜欢
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
相关资源
最近更新 更多