Jump to content

Joins within the same table


playaz

Recommended Posts

Can anyone explain how I perform joins within a single table - I know how to do it with 2 tables but get confused when having to do it a single table.

See the image below
http://img220.imageshack.us/my.php?image=categoryexampleqp8.jpg

I want to display items with the CONCAT command to include the 'parent' category before the 'child' category.

eg. so it displays like this:
IT -> Desktop Technician
Legal -> Solictor
Medical -> Nurse

Can anyone explain how I achieve this with joins and the concat command.

Thanks in advance :)


Thanks in advance
Link to comment
Share on other sites

You simply need to use table aliases to uniquely identify each "instance" of this table... I'm assuming you don't want the main categories, and your table is indexed properly.
[code]
SELECT CONCAT( p.category, ' -> ', c.category )
FROM yourTable AS c
INNER JOIN yourTable AS p ON ( p.category_id = c.parent_id )
WHERE c.parent_id > 0
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.