Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL: Multiple subqueries and where in join clauses
    primarykey
    data
    text
    <p>So I have three tables;</p> <p><code>grandparent</code>, <code>parent</code>, and <code>child</code>.</p> <p><code>parent</code> and <code>child</code> each have a <code>parent</code> field.</p> <p>I want to find all the grandchildren of the grandparent where all the grandchildren are married and the grandparent is over 80. Lets assume each grandparent can only have 4 grandchildren.</p> <p>So I start with a select on the grandparent table:</p> <pre><code>select id from grandparent where age &gt; 80 </code></pre> <p>This will give me a set of grandparents whose age is greater than 80.</p> <p>Then I find the parents in the parent table grouped by their grandparent;</p> <pre><code>select group_concat(id) as grandparent from parent where parent in ( select id from grandparent where age &gt; 80 ) group by parent </code></pre> <p>This gives me a set of parents for each row of grandparents, something like</p> <pre><code>grandparent: (1,2,3,4) grandparent: (5,6,7,8) grandparent: (9,10,11,12) </code></pre> <p>Now I want to find all the children of these parents where they are all married, so far I have something like;</p> <pre><code>select group_concat(parent) as parent group_concat(id) as children from child where parent in ( select group_concat(id) as grandparent from parent where parent in ( select id from grandparent where age &gt; 80 ) group by parent ) and relationship_status = "MARRIED" having count(id) = 4; </code></pre> <p>But obviously it doesn't work because I need the in clause to search the sets of each row returned from the previous query.</p> <p>How would I run the above query for each row set?</p> <p>Note: These aren't the real names of my tables or fields, just removing domain knowledge so it's hopefully easier to help.</p>
    singulars
    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. This table or related slice is empty.
    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