Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks to eggyal for putting me on the right track. Seeing the relative complexity I do not feel so ashamed anymore that I could not crack it in the first go. </p> <p>I ended up doing some research and found some nice setups to used closure tables in mysql. I ended up creating a stored procedure to populate my closure table and of course the new table cust_closure. I renamed by links table to cust_links.</p> <p><strong>cust_links:</strong></p> <pre><code>+-------------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | customer_id | int(11) | YES | | NULL | | | linked_id | int(11) | YES | | NULL | | +-------------+---------+------+-----+---------+----------------+ </code></pre> <p><strong>cust_closure:</strong></p> <pre><code>+-------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+-------+ | customer_id | int(11) | YES | | NULL | | | linked_id | int(11) | YES | | NULL | | | distance | int(11) | YES | | NULL | | +-------------+---------+------+-----+---------+-------+ </code></pre> <p>And then added the stored procedure:</p> <pre><code>CREATE PROCEDURE populate_cust_closure() BEGIN DECLARE distance int; TRUNCATE TABLE cust_closure; SET distance = 0; -- seed closure with self-pairs (distance 0) INSERT INTO cust_closure (customer_id, linked_id, distance) SELECT customer_id, customer_id, distance FROM cust_links GROUP BY customer_id; -- for each pair (root, leaf) in the closure, -- add (root, leaf-&gt;child) from the base table REPEAT SET distance = distance + 1; INSERT INTO cust_closure (customer_id, linked_id, distance) SELECT cust_closure.customer_id, cust_links.linked_id, distance FROM cust_closure, cust_links WHERE cust_closure.linked_id = cust_links.customer_id AND cust_closure.distance = distance - 1; UNTIL ROW_COUNT()=0 END REPEAT; END // </code></pre> <p>When I then called the stored procedure it produced:</p> <pre><code>mysql&gt; select * from cust_closure; +-------------+-----------+----------+ | customer_id | linked_id | distance | +-------------+-----------+----------+ | 1 | 1 | 0 | | 3 | 3 | 0 | | 7 | 7 | 0 | | 13 | 13 | 0 | | 1 | 5 | 0 | | 1 | 2 | 0 | | 1 | 11 | 0 | | 1 | 13 | 0 | | 13 | 14 | 0 | | 1 | 14 | 1 | | 3 | 4 | 0 | | 7 | 8 | 0 | +-------------+-----------+----------+ </code></pre> <p>So now my original query becomes:</p> <pre><code>SELECT username, firstname, lastname, email, active, level FROM customers WHERE id IN (SELECT linked_id FROM cust_closure WHERE customer_id=1); </code></pre> <p>Thanks again for eggyal and hope this helps someone in the future.</p>
    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. 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