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 |
|
zubinbalsara
Starting Member
2 Posts |
Posted - 2006-07-10 : 22:27:02
|
| CART___________________CartID ProductID1 152 173 153 243 89KITS________________________________________________Kitid(PK)ParentProductID ChildProductID1 15 562 15 583 15 594 17 56PRODUCTS____________________________ProductID ProductName15 Insurance Kit16 Television Kit17 Paper Clips24 Towels56 Life Insurance58 Car Insurance59 Home Insurance89 Telephone Directory My query is regarding the above 3 tables.My objective is that given a cartID, I should be able to find the name of the parent product and the name of all the childproducts associated with that parentproduct. For example if the cartid is 1Than my answer should be Insurace Kit (this is the parent Product) Life Insurance (this is the child Product) Car Insurance (this is the Child Product) Home Insurance (this is the child Product) |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-07-10 : 22:30:58
|
| did you try to JOIN the tables?select...from table1inner join table2 on table1.pk1=table2.fk1...where...--------------------keeping it simple... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-11 : 07:52:24
|
Here you go, zubinbalsara. Make sure you have a read of the links madhivanan provided, and make sure you understand the solution below...--datadeclare @CART table (CartID int, ProductID int)insert @CART select 1, 15union all select 2, 17union all select 3, 15union all select 3, 24union all select 3, 89declare @KITS table (Kitid int primary key, ParentProductID int, ChildProductID int)insert @KITS select 1, 15, 56union all select 2, 15, 58union all select 3, 15, 59union all select 4, 17, 56declare @PRODUCTS table (ProductID int primary key, ProductName varchar(20))insert @PRODUCTS select 15, 'Insurance Kit'union all select 16, 'Television Kit'union all select 17, 'Paper Clips'union all select 24, 'Towels'union all select 56, 'Life Insurance'union all select 58, 'Car Insurance'union all select 59, 'Home Insurance'union all select 89, 'Telephone Directory'--inputsdeclare @CartID intset @CartID = 1--calculationselect ProductName + ' (this is the parent Product)'from @CART c inner join @PRODUCTS p on c.ProductID = p.ProductIDwhere CartID = @CartIDunion allselect ProductName + ' (this is the child Product)'from @CART c inner join @KITS k on c.ProductID = k.ParentProductID inner join @PRODUCTS p on k.ChildProductID = p.ProductIDwhere CartID = @CartID/*results------------------------------------------------- Insurance Kit (this is the parent Product)Life Insurance (this is the child Product)Car Insurance (this is the child Product)Home Insurance (this is the child Product)*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|