【问题标题】:search parent with 2 node使用 2 个节点搜索父级
【发布时间】:2016-11-15 09:22:12
【问题描述】:

我有示例层次结构:

id   name    parent 
1    a       null
2    b       1
3    c       2
4    d       2
5    e       2
6    f       1
7    g       6
8    h       6
9    i       6

如果我想在“c”和“g”之间或“c”和“e”之间搜索父母。我怎么做? 我已经尝试过来自here 的脚本 但是我找不到解决问题的方法,有人可以帮助我吗?我不是算法专家,请帮忙处理php代码

我想要输出

如果“c”和“g”我想输出“a”,如果“c”和“e”我想输出“b”

【问题讨论】:

  • parent between "c" and "g" 是什么意思,它应该返回"a,b,f" 吗?你能添加预期的输出吗?
  • 如果“c”和“g”我想返回“a”,如果“c”和“e”我想返回“b”

标签: php mysql hierarchy


【解决方案1】:

使用此查询获取父级。

SELECT name from hierarchytbl where id IN (SELECT Min(parent) 
   FROM hierarchytbl WHERE parent != null and name BETWEEN 'c' and 'g')

【讨论】:

    【解决方案2】:

    mysql> SELECT name from hier where id IN (SELECT Min(parent) FROM hier WHERE name BETWEEN 'c' and 'g');
    +------+
    |名称 |
    +------+
    |一个 |
    +------+
    一组中的 1 行(0.00 秒)

    mysql> SELECT name from hier where id IN (SELECT Min(parent) FROM hier WHERE name BETWEEN 'c' and 'e');
    +------+
    |名称 |
    +------+
    | b |
    +------+ 一组中的 1 行(0.00 秒)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多