2013-11-25 75 views
0

我有2個表有一個共同的柱材料:SQL:我想從表1中選擇所有列,其中columnA在表1等於ColumnB在表2

Table1 

MaterialGroup | Material | MaterialDescription | Revenue 

MG1| DEF| Desc1| 12 

MG2| ABC| Desc2| 13 

MG3| XYZ| Desc3| 9 

MG4| IJK| Desc4| 5 


Table2 
Vendor | VendorSubgroup| Material| Category 

KM1| DPPF| ABC| Cat1 

KM2| DPPL| XYZ| Cat2 

所以,我想選擇的所有記錄表1其中table1的材料匹配材料在表2

在上述情況下,我想這個結果,因爲材料「ABC」和「XYZ」存在於表2:

MG2| ABC| Desc2| 13 

MG3| XYZ| Desc3| 9 

回答

1
SELECT T1.* 
FROM TABLE1 AS T1 
INNER JOIN TABLE2 AS T2 
ON T1.MATERIAL = T2.MATERIAL 
2
SELECT * FROM Table1 AS t1 
WHERE t1.Material IN 
(
    SELECT DISTINCT t2.Material 
    FROM Table2 AS t2 
) 
相關問題