如何比较两个表并在sql中检索匹配和不匹配的数据?

时间:2018-11-12 09:04:42

标签: sql sql-server

我有两个具有不同列名的表,并且大多数值都相同。我想要其他表中匹配和不匹配的数据。

***
src-table
--------------------------------------------------------------------------------
eid  | ename      | email                   |  country
--------------------------------------------------------------------------------
 1     as           as@gmail.com                 india
 2     bs           bs@gmail.com                 usa
 3     cs           cs@gmail.com                 usa
 4     ds           ds@gmail.com                 india
--------------------------------------------------------------------------------
tgt_table
--------------------------------------------------------------------------------
eid  | ename      | email                   |  country
--------------------------------------------------------------------------------
 1     as           as@gmail.com                 india
 2     bs           b@gmail.com                 india
 3     cs           cs@gmail.com                 usa
 4     ds           d@gmail.com                 india
--------------------------------------------------------------------------------
expected output  for matched_data 
--------------------------------------------------------------------------------
src_coloumnname | src_data           | tgt_colomnname    | tgt_data
--------------------------------------------------------------------------------
eid                  1                   eid                  1
eid                  2                   eid                  2
eid                  3                   eid                  3
eid                  4                   eid                  4
ename                as                  ename                as 
ename                bs                  ename                bs
ename                cs                  ename                cs
ename                ds                  ename                ds
email            as@gmail.com            email             as@gmail.com
email            cs@gmail.com            email             cs@gmail.com
country          india                   country            india
country          usa                     country            usa
country          india                   country            india
----------------------------------------------------------------------------
***
  • 相似的不匹配记录 我该如何实现?有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

可以一次使用一列:

SELECT 'eid' AS match_column, l.eid AS src_value, r.eid AS tgt_value, CASE WHEN l.eid = r.eid THEN 'match' ELSE 'no match' END AS result
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid

UNION ALL

SELECT 'ename', l.ename, r.ename, CASE WHEN l.ename = r.ename THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid

UNION ALL

SELECT 'email', l.email, r.email, CASE WHEN l.email = r.email THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid

UNION ALL

SELECT 'country', l.country, r.country, CASE WHEN l.country = r.country THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid

它匹配行匹配的列。它不会检查任何一个表中缺少的行。

答案 1 :(得分:0)

您可以进行简单的加入,我想您可以做到

SELECT * FROM T1 FULL OUTER JOIN T2 ON TRUE 

或     选择*从T1,T2

每一次

选择*从T1外部连接T2在T1.C1上<> T2.T1_C1 对于无与伦比的

选择*从T1外部连接T2到T1.C1 = T2.T1_C1 用于匹配

您还可以更改联接类型以进行匹配 在这里加入文档 https://www.w3schools.com/sql/sql_join.asp 您也可以

SELECT T1.C1, T2.C1,T1.C2, ..., CASE WHEN T1.C1=T2.T1_C1 THEN 'MATCH' ELSE 'NOT MATCH' END FROM T1,T2

为您提供匹配结果的结果 这是案件的文件 https://www.w3schools.com/sql/sql_case.asp