【问题标题】:SQL: I want to select all columns from table1 where columnA in table1 is equal to ColumnB in table2SQL:我想从 table1 中选择所有列,其中 table1 中的 columnA 等于 table2 中的 ColumnB
【发布时间】:2013-12-10 18:32:20
【问题描述】:

我有 2 个表,它们有一个共同的列 Material:

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 中的材料与 table2 中的材料匹配

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

MG2| ABC| Desc2| 13

MG3| XYZ| Desc3| 9

【问题讨论】:

    标签: mysql sql subquery


    【解决方案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
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多