#SQL#查询2个表中某相同字段在两个表的存在情况。 字段相同,内容不一样, 比如A表 中有 abc B表中有bcd

查询2个表中某相同字段在两个表的存在情况。
字段相同,内容不一样,
比如A表 中有 abc
B表中有bcd
希望统计的结果为:

-----------------------------------
某字段 表一 表二
-----------------------------------
a V
-----------------------------------
b V V
-----------------------------------
c V V
-----------------------------------
d V
-----------------------------------

select field_set.field,
caes when tb_a.field is not NULL then 'V' else '' end as [in tb_a],

caes when tb_b.field is not NULL then 'V' else '' end as [in tb_a]
from (

select distinct field from tb_a
union

select distinct field from tb_b
) as field_set
left join tb_a on tb_a.field = field_set.field
left join tb_b on tb_b.field = field_set.field
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-08
Select name,'√' 表1,'' 表2 from syscolumns Where ID=OBJECT_ID('表1')
union all
Select name,'','√' from syscolumns Where ID=OBJECT_ID('表2')