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.
Author |
Topic |
sidharth.banyal
Starting Member
11 Posts |
Posted - 2010-09-19 : 08:47:26
|
I have a category sub category table as:-Catidcatnameparentcatidparentcatid is foreign key to subcategories. It stores catid of parent category.In my products table i am storing catid for category. Now when i am searching products, i need to show products whose category is child of any category.EX:-i have a categoryCAT1subcat1 is child of cat1subcat2 is child of subcat1then if i select cat1 then all products whether they are under cat1 subcat1 or subcat2 should display.If i select sucat1 then all products under subcat1 osubcat2 should display.Any help will be highly appreciated.Thanks in advance.Sidharth Banyal |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
sidharth.banyal
Starting Member
11 Posts |
Posted - 2010-09-21 : 00:25:48
|
Pleas give me an exampleSidharth Banyal |
 |
|
PavanKK
Starting Member
32 Posts |
Posted - 2010-09-21 : 03:10:40
|
hi Sidharth,The link provided by Visakh is having the required examples.For your category search you can use the following query.---DECLARE @CatID VARCHAR(20)SET @CatID = 'CAT1';WITH Cte AS(SELECT *FROM ProductsWHERE catid = @CatIDUNION ALLSELECT P.*FROM category CJOIN Cte Ct ON C.parentcatid = Ct.CatidJOIN Products P ON C.Catid = P.Catid)SELECT * FROM Cte------KK |
 |
|
|
|
|