Hi all i need a help i need to display a grand child from parent id that means one id=2 has a child id=3 and child id=3 has a child id=4 (here child id=3 is parent id for child id=4).
below is my tables:
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
from above three tables i need to display like id 3217 has id 3228 i need a query .i am able to write a query for only child id not for grand child .any one help to sort out this
This gives you the parent and grand child info:
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`
EDIT
This gives you the parent, child and grand child info:
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`
You can easily join the three tables by matching the right columns to each other:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.