在SQL中查找2个表的不匹配行

时间:2015-06-16 13:36:35

标签: sql sql-server join

我有2个表,其中包含3列,这些表具有相同的信息。 我想要一个只选择没有完整行匹配的行的查询。下面是我想要匹配的2个表的示例:

表1

   ID   FPRICE  FPRODUCT
   1      1       A
   2      2       B
   3      3       C
   4      4       D
   5      5       F

表2

   ID   TPRICE  TPRODUCT
   1      1       A
   2      2       B
   3      3       C
   4      5       D
   6      6       E 

期望的输出:

   ID   FPRICE  FPRODUCT   TPRICE   TPRODUCT
   4      4       D          5          D
   5      5       F          NULL       NULL
   6     NULL     NULL       6          E

6 个答案:

答案 0 :(得分:1)

更容易验证我们是否构建了一些DDL并填充了样本数据,但我认为这样做会。它需要一个完整的连接来查找部分匹配的记录,然后过滤掉完全匹配的记录。

sqlfiddle.com

CREATE TABLE Table1 (ID INT, FPRICE INT, FPRODUCT CHAR(1))  
INSERT INTO Table1 (ID,FPRICE,FPRODUCT) VALUES
   (1,      1,       'A')
  ,(2,      2,       'B')
  ,(3,      3,       'C')
  ,(4,      4,       'D')
  ,(5,      5,       'F')   


CREATE TABLE TABLE2 (ID INT, TPRICE INT, TPRODUCT CHAR(1))
INSERT INTO Table2 (ID,TPRICE,TPRODUCT) VALUES
   (1,      1,       'A')
  ,(2,      2,       'B')
  ,(3,      3,       'C')
  ,(4,      5,       'D')
  ,(6,      6,       'E')   

SELECT * 
  FROM Table1 t1
       FULL JOIN 
       Table2 t2 ON t1.ID = t2.ID  
                 --EDIT: remove to exactly match the desired output
                 --OR t1.FPRICE = t2.TPRICE  
                 --OR t1.FPRODUCT = t2.TPRODUCT
 WHERE NOT (    t1.ID = t2.ID 
            AND t1.FPRICE = t2.TPRICE 
            AND t1.FPRODUCT = t2.TPRODUCT)
       OR (   COALESCE(t1.ID,t1.FPRICE,T1.FPRODUCT) IS NULL
           OR COALESCE(t2.ID,t2.TPRICE,T2.TPRODUCT) IS NULL)

答案 1 :(得分:0)

你可以尝试这个查询吗?

SELECT DISTINCT t1.ID t1.FPRICE t1.FPRODUCT
from Table1 t1 LEFT JOIN Table2 ON 
Table1.ID = Table2.ID AND Table1.FPRICE = Table2.TPRICE AND Table1.FPRODUCT= Table2.TPRODUCT
WHERE 
Table1.ID is NULL or Table1.FPRICE is NULL or Table1.FPRODUCT is NULL 
or Table2.TPRICE is NULL or Table2.TPRODUCT is NULL

答案 2 :(得分:0)

使用full join

select * 
from Table1 t1
full join Table2 t2 on t1.ID = t2.ID and 
                       t1.FPRICE = t2.FPRICE /*and 
                       t1.FPRODUCT = t2.FPRODUCT*/
where t1.ID is null or t2.ID is null

答案 3 :(得分:0)

看起来你想匹配id'然后比较值:

select t1.id, t1.fprice, t1.fproduct, t2.fprice, t2.fproduct
from Table1 t1 full join
     Table2 t2
     on t1.ID = t2.ID 
where (t1.ID is null or t2.ID is null) or -- no match as all
      (t1.FPRICE <> t2.FPRICE or
       t1.FPRODUCT <> t2.FPRODUCT
      );

如果列值可能为where,则NULL子句会更复杂,但您的示例数据没有NULL的任何示例。

答案 4 :(得分:0)

SELECT *
FROM Table1 t1
FULL JOIN Table2 t2 ON t2.ID = t1.ID
WHERE
    t2.TPRICE <> t1.FPRICE
    OR t2.TPRODUCT <> t1.FPRODUCT
    OR t2.TPRICE IS NULL
    OR t1.FPRICE IS NULL
    OR t2.TPRODUCT IS NULL
    OR t1.FPRODUCT IS NULL

答案 5 :(得分:0)

我会将Michael1105的答案格式化为:

SELECT ISNULL(t1.ID,t2.ID),t1.FPRICE,t1.FPRODUCT,t2.TPRICE,t2.TPRODUCT FROM Table_1 t1 FULL JOIN Table_2 t2 ON t2.ID = t1.ID WHERE t2.TPRICE
<>t1.FPRICE OR t2.TPRODUCT
  <>t1.FPRODUCT OR t2.TPRICE IS NULL OR t1.FPRICE IS NULL OR t2.TPRODUCT IS NULL OR t1.FPRODUCT IS NULL

满足您的具体要求。