Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have a known limited depth, you can unroll the recursion and use a stored procedure or view. For MySQL, the following work:</p> <h3>Stored Routine solution:</h3> <pre><code>DELIMITER $$ CREATE PROCEDURE GetRelatedPersonsWithPersonId( IN pId VARCHAR(36)) BEGIN select * from Person where idPerson in ( select ParentId from ParentChildRelationship where ParentId = pId union select ChildID from ParentChildRelationship where ParentId = pId union select ChildID from ParentChildRelationship where ParentId in (select ChildID from ParentChildRelationship where ParentId = pId) union select ChildID from ParentChildRelationship where ParentId in (select ChildID from ParentChildRelationship where ParentId in (select ChildID from ParentChildRelationship where ParentId = pId)) ) ; END $$ </code></pre> <h3>View solution:</h3> <pre><code>Create view ChildRecurse As Select ParentId, ChildID from ParentChildRelationship Union Select x1.ParentId, x2.ChildId from ParentChildRelationship x1 Inner join ParentChildRelationship x2 on x2.ParentId = x1.ChildId Union Select x1.ParentId, x3.ChildId from ParentChildRelationship x1 Inner join ParentChildRelationship x2 on x2.ParentId = x1.ChildId Inner join ParentChildRelationship x3 on x3.ParentId = x2.ChildId Union Select x1.ParentId, x4.ChildId from ParentChildRelationship x1 Inner join ParentChildRelationship x2 on x2.ParentId = x1.ChildId Inner join ParentChildRelationship x3 on x3.ParentId = x2.ChildId Inner join ParentChildRelationship x4 on x4.ParentId = x3.ChildId </code></pre> <p>Then select as such: </p> <pre><code>select * from person where idPerson=@ID or idPerson in (select ChildId from ChildRecurse where ParentId=@ID) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload