如何与大孩子一起展示孩子?

时间:2018-03-22 13:36:21

标签: php mysql sql

我正在获得所有大孩子的头衔,但我没有得到孩子的头衔我请帮助我两个人

这是我的查询:

SELECT g.title  FROM object_data a,`object_reference` b,`tree` c,`object_reference` d,`tree` e,`object_reference` f,`object_data` g WHERE a.`obj_id` = b.`obj_id` AND b.`ref_id` = c.`parent` AND c.`child` = d.`ref_id` AND d.`ref_id` = e.`parent` AND e.`child` = f.`ref_id` AND f.`obj_id` = g.`obj_id`AND g.type='tst' AND a.obj_id=3217

这里是样本数据: table object_data:

obj_id | type | title 
-------+------+------
3217   |crs   |it 
3221   |grp   |xyz 
3228   |tst   |test 
3264   |tst   |test3 

table object_reference:

ref_id | obj_id 
-------+---------
337    |3217       
338    |3221      
343    |3228 
371    |3264

表树:

tree | child | parent 
-----+-------+------
1    |338    |337 
2    |343    |338 
3    |371    |337

这是预期的结果:

title
-----
test
test3

但是我从上面的查询中得到的只是test3

2 个答案:

答案 0 :(得分:0)

我假设您正在寻找没有孩子的所有ID标题,因为您不想看到XYZ是测试的父母。 如果我的假设是正确的,那么以下查询有助于获得所需的结果。

select title from object_data c , object_reference d
where c.obj_id=d.obj_id
and d.ref_id in (select a.child from tree a, tree b
where a.child =b.parent(+)
and b.parent is null);

结果:

标题

测试

TEST3

要显示给定课程(树的根)下的所有测试,请执行以下查询

with parent_child (child, parent) as
(
select child, parent from tree 
union all
select t.child, parent_child.parent 
from parent_child , tree t where t.parent=parent_child.child)
select c.title from parent_child a,object_data c , object_reference d
where c.obj_id=d.obj_id
and d.ref_id=a.child
and parent=337 --Provide the root id
and type='tst'; --removing this would display all the nodes of a tree, including XYZ

答案 1 :(得分:0)

由于您的问题不明白,请尝试以下方法:

select a.title from object_data as a 
         inner join object_reference b on a.obj_id = b.obj_id
         inner join tree as c on b.ref_id = c.child

如果这不是你想要的,请更好地说明。