【问题标题】:Return from Table 2 different Ids from same names on Table1从表 2 返回来自 Table1 上相同名称的不同 Id
【发布时间】:2015-11-30 16:57:59
【问题描述】:

我一直在尝试开发一个查询来解决问题,但这很难。

表 1:

+------+----+
| NAME | ID |
+------+----+
| A    |  1 |
| A    |  2 |
| B    |  1 |
| B    |  5 |
| C    |  8 |
+------+----+

表 2:

+------+----+
| NAME | ID |
+------+----+
| A    |  1 |
| A    |  4 |
| B    |  3 |
| B    |  5 |
| D    |  9 |
+------+----+

根据这些结果,我需要返回表 2 中名称包含在表 1 中而 ID 不包含的所有内容。

所以,这个例子,返回应该是:

+------+----+
| NAME | ID |
+------+----+
| A    |  4 |
| B    |  3 |
+------+----+

【问题讨论】:

    标签: sql oracle inner-join outer-join


    【解决方案1】:

    你可能想试试这个:

    编辑:用 WITH 子句中的简单子查询替换 table1 和 table2。

    WITH table1 AS
    (
    SELECT
      DECODE(LEVEL,1, 'A',2, 'A',3, 'B',4, 'B',5, 'C') AS name
      ,DECODE(LEVEL,1, 1,2, 2,3, 1,4, 5,5, 8) AS id
    FROM
      dual
    CONNECT BY LEVEL < 6
    )
    ,table2 AS
    (
    SELECT
      DECODE(LEVEL,1, 'A',2, 'A',3, 'B',4, 'B',5, 'D') AS name
      ,DECODE(LEVEL,1, 1,2, 4,3, 3,4, 5,5, 9) AS id
    FROM
      dual
    CONNECT BY LEVEL < 6
    )
    SELECT
      t2.id
      ,t2.name
    FROM
      table1 t1
      ,table2 t2
    WHERE
      t1.name = t2.name -- here we take all the records from table2, which have the same names as in table1
    MINUS -- then we "subtract" the records that have both the same name and id in both tables
    SELECT
      t2.id
      ,t2.name
    FROM
      table1 t1
      ,table2 t2
    WHERE
      t1.name = t2.name
      AND t1.id = t2.id
    

    【讨论】:

    • 不退货..会尝试发现问题出在哪里
    • 嘿。用一些示例 table1 和 table2 更新了我的查询。
    • @AndrewMcCoist FWIW,您的查询对我来说效果很好,至少当我针对我用于回答的同一数据集运行它时。我不完全确定为什么 OP 似乎对似乎符合他们要求的答案有问题!
    【解决方案2】:

    我愿意:

    with t1 as (select 'A' name, 1 id from dual union all
                select 'A' name, 2 id from dual union all
                select 'B' name, 1 id from dual union all
                select 'B' name, 5 id from dual union all
                select 'C' name, 8 id from dual),
         t2 as (select 'A' name, 1 id from dual union all
                select 'A' name, 4 id from dual union all
                select 'B' name, 3 id from dual union all
                select 'B' name, 5 id from dual union all
                select 'D' name, 9 id from dual)
    select name, id
    from   t2
    where  name in (select name from t1)
    minus
    select name, id
    from   t1;
    
    NAME         ID
    ---- ----------
    A             4
    B             3
    

    【讨论】:

    • 它返回 t1 没有的 NAMES。并且查询应该只显示 T2 具有的 ID 和 T1 没有相同名称的 ID。就像例子一样。
    • 我的查询以什么方式返回 t1 没有的名称?!如果是这种情况,结果将包括 (name, id) = ('D', 9),但它们没有。根据您的问题,我的 (name, id) = ('A', 4) 和 ('B', 3) 结果与您想要的结果完全匹配!
    • 我在转换为正确格式时犯了一个错误。它只是在这里工作。
    【解决方案3】:

    您可以使用 NOT EXISTS 或类似的:

    SELECT  t2.*
    FROM Table2 t2
    WHERE NOT EXISTS
    (
       SELECT 1
       FROM Tabl1 t1
       WHERE t1.Name = t2.Name
       AND t1.Id = t2.Id
    );
    

    【讨论】:

      【解决方案4】:
      SELECT T1.ID,T1.NAME
      FROM TABLE2 T1 INNER JOIN TABLE1 T2 ON T1.NAME = T2.NAME
                     LEFT JOIN TABLE1 T3 ON T3.ID = T1.ID
      WHERE T3.ID IS NULL 
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多