-2

Anyone, please help me with the SQL query for traversing the tree. I have a table called users, which has the columns id, name , and parent.

Please, help me with a SQL query that can traverse the tree from the desired root.

EDIT Hi i got this URL that seems to be solved.

Deeply nested subqueries for traversing trees in MySQL

But i don't have knowledge of JOIN, Thats y i cant construct the query with my table :-( :-(

Community
  • 1
  • 1
shawn
  • 137
  • 9
  • 1
    What do you want to get out of it? – Shef Sep 17 '11 at 16:52
  • what is the table structure, table data and expected output ? And what have you tried till now ? – DhruvPathak Sep 17 '11 at 16:54
  • You need to understand JOIN to use SQL. JOIN is as fundamental to SQL as `foreach` is to PHP. Try reading fearless leader's [A Visual Explanation of SQL Joins](http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html) – Bill Karwin Sep 18 '11 at 02:14

1 Answers1

4

MySQL does not support recursive queries. Most other brands of database support recursive query syntax with the common table expression syntax (queries beginning with WITH).

To solve tree traversal queries in MySQL, you need to store the data differently so you can query whole trees in one query. There are several solutions for this:

  • Path Enumeration
  • Nested Sets
  • Closure Table

For details, see:

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828