我想知道以下情况的查询。
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应返回的查询:
最后,我应该得到以下结果:,
Table1.Col1 Table1.col2 Table2.col2
Sandy 1 24
Reena 2 32
Karthik NA 5
Muthu NA 6提前谢谢。
发布于 2013-10-29 11:24:32
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-29 11:26:21
请尝试:
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发布于 2015-02-12 10:06:33
选择c3,ISNULL(C2,'NA')作为C2,从Table1右联接选择c4
Table2 on Table1 .c1 =表2.C3,其中ISNULL(表1.c2,0) <>表2.C4
https://stackoverflow.com/questions/19656749
复制相似问题