MYSQL hierarchical recursive query method creation method
Answer
Use with recursive syntax:
<code class="language-sql">WITH RECURSIVE cte (id, name, parent_id) AS ( SELECT id, name, parent_id FROM products WHERE parent_id = 19 UNION ALL SELECT p.id, p.name, p.parent_id FROM products p INNER JOIN cte ON p.parent_id = cte.id ) SELECT * FROM cte;</code>
Neilian variables:
Path style identifier:
The ID value of allocating hierarchical information (path):
<code class="language-sql">SELECT id, name, parent_id FROM (SELECT * FROM products ORDER BY parent_id, id) products_sorted, (SELECT @pv := '19') initialisation WHERE FIND_IN_SET(parent_id, @pv) AND LENGTH(@pv := CONCAT(@pv, ',', id))</code>
Then use this query:
The above is the detailed content of How to Retrieve All Child IDs in a MySQL Hierarchical Structure Using a Single Query?. For more information, please follow other related articles on the PHP Chinese website!