faqts : Computers : Databases : MySQL : Common Problems : Unusual Queries

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 4 people (50%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

i have master and child relationship in a table.. where one record is child of another record. how can i display hierarchy with minimum query.

Mar 15th, 2004 11:40
Matt Chatterley, nitin nitin,


A simple example:
Assume a table Table1 with columns ItemID, ParentID, Description.
SELECT P.Description, I.Description
FROM Table1 I
INNER JOIN Table1 P ON P.ItemID = I.ItemID
Please note that this will only show items which _have a parent_.
To show all items, you would need something like:
SELECT CASE WHEN P.Description IS NULL THEN 'No Description' ELSE
P.Description END AS Parent, I.Description AS Child
FROM Table1 I
LEFT JOIN Table2 P ON P.ItemID = I.ParentID