Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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

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
如何在php中从父母那里得到孙子_Php_Mysql - Fatal编程技术网

如何在php中从父母那里得到孙子

如何在php中从父母那里得到孙子,php,mysql,Php,Mysql,嗨,我需要一个帮助我需要显示一个来自父id的孙子,这意味着一个id=2的子id=3,子id=3的子id=4,这里子id=3是子id=4的父id 以下是我的表格: table object_data: obj_id | type | title --- |------|------ 3217 |crs |it 3221 |grp |xyz 3228 |tst |test table object_reference: ref_id | obj_id ---

嗨,我需要一个帮助我需要显示一个来自父id的孙子,这意味着一个id=2的子id=3,子id=3的子id=4,这里子id=3是子id=4的父id

以下是我的表格:

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,我需要一个查询。我能够为独生子女id而不是grand child编写一个查询。任何一个有助于解决此问题的查询都可以通过将正确的列相互匹配来轻松地联接这三个表:

SELECT tree.child
FROM object_data
JOIN object_reference ON object_data.obj_id = object_reference.obj_id
JOIN tree ON tree.parent = object_reference.ref_id
WHERE object_data.obj_id = 3217

这将为您提供家长和孙子女信息:

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`
编辑

这将为您提供父、子和孙儿信息:

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`

通过此查询,我没有得到grand child,我只得到338个子id。我需要得到子id 343,然后用WHERE object_data.obj_id=3221替换最后一行。无论您从object_数据中指定什么对象ID,您都将从树中获得相关的子对象。但343不是3217的孙子。343是338的孩子,338是3221的孩子,所以当你查找3221时,你会得到343。当您查找3217时,您会得到338。非常感谢您帮助我了解您的知识。它在数据库phpmyadmin上运行良好。现在我将尝试用php脚本编写。如果我遇到任何错误,我将与您联系brohi先生,我也需要显示child tiitle。您能帮助我吗?请