SQL:我想从table1中选择所有列,其中table1中的columnA等于table2中的ColumnB

时间:2013-11-25 20:55:46

标签: mysql sql subquery

我有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

所以,我想从table1中选择table1中的Material与table2中的Material匹配的所有记录

在上面的场景中,我想要这个结果,因为材料“ABC”和“XYZ”出现在表2中:

MG2| ABC| Desc2| 13

MG3| XYZ| Desc3| 9

2 个答案:

答案 0 :(得分:2)

SELECT * FROM Table1 AS t1
WHERE t1.Material IN
(
  SELECT DISTINCT t2.Material 
  FROM Table2 AS t2
)

答案 1 :(得分:1)

SELECT T1.*
FROM TABLE1 AS T1
INNER JOIN TABLE2 AS T2
ON T1.MATERIAL = T2.MATERIAL