0

我想知道以下情况的查询。

Table 1 :         Table 2

Col1    Col2    Col1    Col2
Sandy    1     Sandy     24
Reena    2     Reena     32
Swathi   3     Swathi    3
Reenu    4     Karthik   5
               Reenu     4
               Muthu     6

查询应返回:

  1. 表 2 中不在表 1 中的行(结果应该是 Karthik 和 Muthu 行)

  2. 它应该比较列组合并返回更改后的列组合。

最后我应该得到以下结果:

Table1.Col1 Table1.col2 Table2.col2
Sandy         1          24
Reena         2          32
Karthik      NA          5
Muthu        NA          6

提前致谢。

4

4 回答 4

1
select t2.col1, t1.col2, t2.col2 as t2c
from table2 t2
left join table1 t1 on t1.Col1 = t2.Col1
where t1.Col2 is null or t1.Col2 <> t2.Col2

如果你想明确地'NA',你应该做这样的事情(功能取决于你的dbms)

coalesce(<somefunctionToConvertinttostring>(t1.col2), 'NA')

sqlFiddle

于 2013-10-29T11:24:32.250 回答
0

请试试:

select a.Col1, a.Col2, b.Col2 
from Table2 a left join Table1 b on a.Col1=b.Col1 
where b.Col1 is null

union

select a.Col1, a.Col2, b.Col2 
from Table2 a inner join Table1 b on a.Col1=b.Col1 
where a.Col2<>b.Col2
于 2013-10-29T11:26:21.133 回答
0

select c3,ISNULL(C2,'NA') AS C2,c4 from Table1 right join
Table2 on Table1 .c1 = Table2.C3 WHERE ISNULL(Table1.c2,0) <> Table2.C4

于 2015-02-12T10:06:33.140 回答
0

您可以使用以下方式连接两个查询UNION ALL

SELECT Table1.Col1 AS T1Col1, 
       Table2.col1 AS T2Col1, 
       Table2.col2 As T2Col2
FROM Table1 INNER JOIN Table2
  ON Table1.Col1 = Table2.Col1 
WHERE Table1.Col2 <> Table2.Col2

UNION ALL 

SELECT COALESCE(Table1.Col1,'NA') AS T1Col1, 
       Table2.col1 AS T2Col1, 
       Table2.col2 As T2Col2
FROM Table2 LEFT OUTER JOIN Table1
  ON Table1.Col1 = Table2.Col1 
WHERE Table1.Col1 IS NULL

Demonstration

于 2013-10-29T11:28:07.910 回答