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 |
|
charles86
Starting Member
2 Posts |
Posted - 2010-05-26 : 12:16:12
|
| I got 1 situation that I cant solve. Can you all please help me on it ?Table 1Product Type Product DescriptionA AppleB Banana O Orange Table 2Product Type 1 Product Type 2 Product Type 3A A BB A OOutcomeProduct 1 Description Product 2 Description Product 3 DescriptionA Apple A Apple B BananaB Banana A Apple O Orange Here is my problem, Table 1 is a table that store the product type & its description. Table 2 is a table that store different product type in a record.What I need to do is , I have to select all record from Table 2 and get their description from Table 1.Any1 can help me on how to write the SQL query ? |
|
|
marjune
Starting Member
2 Posts |
Posted - 2010-05-27 : 02:39:43
|
| [code]SELECT Table1.ProductType1 ,Table1.ProductType2 ,Table1.ProductType3 ,Table2.ProductDescriptionFROM Table1 JOIN Table2 ON Table1.ProductType1 = Table2.ProductType1[/code] |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-27 : 11:15:47
|
| [code]-- Setup Sample DateDECLARE @Table1 TABLE (ProductType CHAR(1), ProductDescription VARCHAR(50))INSERT @Table1SELECT 'A', 'Apple'UNION ALL SELECT 'B', 'Banana' UNION ALL SELECT 'O', 'Orange'DECLARE @Table2 TABLE (ProductType1 CHAR(1), ProductType2 CHAR(1), ProductType3 CHAR(1))INSERT @Table2SELECT 'A', 'A', 'B'UNION ALL SELECT 'B', 'A', 'O'-- Run QuerySELECT Table2.ProductType1, A.ProductDescription AS ProductDescription1, Table2.ProductType2, B.ProductDescription AS ProductDescription2, Table2.ProductType3, C.ProductDescription AS ProductDescription2FROM @Table2 AS Table2INNER JOIN @Table1 AS A ON Table2.ProductType1 = A.ProductTypeINNER JOIN @Table1 AS B ON Table2.ProductType2 = B.ProductTypeINNER JOIN @Table1 AS C ON Table2.ProductType3 = C.ProductType[/code] |
 |
|
|
charles86
Starting Member
2 Posts |
Posted - 2010-05-30 : 10:35:39
|
| thanks lamprey , i will go to try it out. hope it can work. thx a lot |
 |
|
|
|
|
|
|
|