0

I would like to select a parent and all of its child nodes if any particular child matches a set of criteria. Is this possible with a single query?

Using two queries it would look like this:

SELECT p_id INTO target 
FROM parent, children 
WHERE parent.p_id = children.p_id and children.data1 = 'blah';

SELECT * 
FROM parent 
JOIN children ON parent.p_id = children.p_id and children.p_id = target; 
      

.

p_id    name
1       Perice
2       Siward
3       Scottie

c_id    p_id    name
1          1    Arda
2          1    Katrinka
3          2    Gladi
4          3    Hedi
5          3    Didi
6          3    Lorry

if the predicate was a child name match for Lorry, then the result set would contain

p_id c_id p_name    c_name
3    4    Scottie   Hedi
3    5    Scottie   Didi
3    6    Scottie   Lorry
cubesnyc
  • 1,385
  • 2
  • 15
  • 32

1 Answers1

-1
SELECT parent.*, children.*
FROM parent
INNER JOIN children ON parent.p_id = children.p_id and children.data1 = 'blah';

EDIT after comment:

SELECT parent*
FROM parent
WHERE parent.p_id IN (selet children.p_id 
                       FROM childeren 
                       WHERE children.p_id=parent.p_id and chidren.data1='blah')
Luuk
  • 12,245
  • 5
  • 22
  • 33