Swadesh Behera
Swadesh Behera

Reputation: 927

How to retrieve hierarchical data stored in mysql using sql-query

My Database structure is like follows

id category    parent
-------------------------
1  Programming 
2  Database    Programming
3  MySQL       Database

Now when i query for MySQL, i should get the result as

MySQL -> Database -> Programming

I received some suggestions that, i can do it using Stored Procedures. But it will make my query slow.

Upvotes: 2

Views: 319

Answers (3)

swapnesh
swapnesh

Reputation: 26722

Check this query and let me know if it works or not( testtube is my table name )---

SELECT a.category,  "->", a.parent,  "->", b.parent
FROM testtube AS a
INNER JOIN testtube AS b
WHERE a.parent = b.category
AND a.category =  "MySql"
LIMIT 0 , 30

Upvotes: 0

Sandy8086
Sandy8086

Reputation: 643

means you want to display output

Database -> Programming

this format.

I tried one query

 select category,"->",parent from table where id =2;

it gave output

category ->  parent
Database -> Programming   

Upvotes: 0

s.webbandit
s.webbandit

Reputation: 17000

This is just what JOINs are made for.

Upvotes: 1

Related Questions