Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Simple Join Question

Author  Topic 

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-08-04 : 23:43:39
Hello,

I have a category table. Each row can be a top level category, a "parent" category, or a child cateogry. Cat's that are the children of another category have the "parent_id" column populated with the id of the category above them:

id parent_id global_category_id store_id name description image_src image_name
----------------------------------------------------------------------------------------------------
19 NULL 0 68 Electronics Home Electronics NULL
29 NULL 1 66 Test Category Testing


How can I do a join on this table so that it returns the parent name of the row that has the category.

So basically, I want to include an additional column in the results that says "Parent Category Name" and join the "name" column of the category that is in the parnet_id column.

Thanks

marat
Yak Posting Veteran

85 Posts

Posted - 2007-08-05 : 06:12:53
select Cat.*, Prnt.name as parent_name
from Category Cat
left join Category Prnt on Prnt.id = cat.parent_id
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-08-05 : 22:16:47
Thanks!
Go to Top of Page
   

- Advertisement -