如何在php中从父母那里得到一个大孩子

时间:2018-02-14 01:52:24

标签: php mysql

大家好我需要一个帮助我需要显示一个来自父ID的大孩子,这意味着一个id = 2有一个孩子id = 3而child id = 3有一个孩子id = 4(这里孩子id = 3是父母id为id = 4)。

下面是我的表格:

table object_data:
obj_id | type | title 
---    |------|------
3217   |crs   |it 
3221   |grp   |xyz 
3228   |tst   |test 

table object_reference:
ref_id | obj_id 
---    |---------
337    |3217       
338    |3221      
343    |3228 

table tree:
tree | child | parent 
---  |-------|------
1    |338    |337 
2    |343    |338 

从上面三个表我需要显示像id 3217有id 3228我需要一个查询。我能够只为一个孩子写一个查询而不是为了大孩子。任何一个帮助理清这个

2 个答案:

答案 0 :(得分:1)

这会为您提供父子信息:

SELECT
    a.`type` as `parent_type`,
    a.`title` as `parent_title`,
    g.`type` as `gchild_type`,
    g.`title` as `gchild_title`
FROM `object_data` a
JOIN `object_reference` b
    ON a.`obj_id` = b.`obj_id`
JOIN `tree` c
    ON b.`ref_id` = c.`parent`
JOIN `object_reference` d
    ON c.`child` = d.`ref_id`
JOIN `tree` e
    ON d.`ref_id` = e.`parent`
JOIN `object_reference` f
    ON e.`child` = f.`ref_id`
JOIN `object_data` g
    ON f.`obj_id` = g.`obj_id`

修改

这会为您提供父,子和大孩子信息:

SELECT
    a.`type` as `parent_type`,
    a.`title` as `parent_title`,
    g1.`type` as `child_type`,
    g1.`title` as `child_title`,
    g.`type` as `gchild_type`,
    g.`title` as `gchild_title`
FROM `object_data` a
JOIN `object_reference` b
    ON a.`obj_id` = b.`obj_id`
JOIN `tree` c
    ON b.`ref_id` = c.`parent`
JOIN `object_reference` d
    ON c.`child` = d.`ref_id`
JOIN `object_data` g1
    ON d.`obj_id` = g1.`obj_id`
JOIN `tree` e
    ON d.`ref_id` = e.`parent`
JOIN `object_reference` f
    ON e.`child` = f.`ref_id`
JOIN `object_data` g
    ON f.`obj_id` = g.`obj_id`

答案 1 :(得分:0)

您可以通过将右列相互匹配来轻松加入三个表:

var arr1 = ["A", "B"];

var arr2 = ["A", "B"];

// Where the result should be
var arrayOfObjects = [
  {
    arr1: "A",
    arr2: "A"
  },
  {
    arr1: "B",
    arr2: "B"
  }

];