查询比较两个表的相同数据和不同数据问题?

liguochun 2008-07-16 12:17:48
有两个表,table1,table2

里面字段相同,都有Tel字端

查询比较两个表的Tel字端相同数据和不同数据

1.比较两个表,查出不同的Tel字端值的数据
2.比较两个表,查出相同的Tel字端值的数据


请高手帮忙,谢谢
...全文
133 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lff642 2008-07-16
  • 打赏
  • 举报
回复
1.比较两个表,查出不同的Tel字端值的数据
-->
比如A表,B表
a.先找出在A表中有,但是在B表中没有的数据,
b.再找出在B表中有.但是在A表中没有的数据,
c.合并查询出来的数据,这个就是你要的结果了.

2.比较两个表,查出相同的Tel字端值的数据
-->
联接两表条件是TEL字段值的数据相同.


纯属个人想法 .高手继续.
npkaida 2008-07-16
  • 打赏
  • 举报
回复
select tel from table1 where tel in (select tel from table2)

select tel from table1 where tel not in (select tel from table2)
sdxiong 2008-07-16
  • 打赏
  • 举报
回复
查找不同数据:
select * from table1 a where not exists(select 1 from table2 where tel=a.tel)

select * from table2 a where not exists(select 1 from table1 where tel=a.tel)

查找相同数量:
select * from table1 a where exists(select 1 from table2 where tel=a.tel)
中国风 2008-07-16
  • 打赏
  • 举报
回复
2000:

select * from t1 where not exists(select 1 from t2 where col=t1.col)--
union all
select * from t2 where not exists(select 1 from t1 where col=t2.col)--


select
t1.*
from
t1
join
t2 on t1.col=t2.col
中国风 2008-07-16
  • 打赏
  • 举报
回复
select *,[表]='t1有t2沒'
from
(select * from t1
except
select * from t2)t1
union all
select *,[表]='t2有t1沒'
from
(select * from t2
except
select * from t1)t2


----------相同
select * from t1
intersect
select * from t2


liguochun 2008-07-16
  • 打赏
  • 举报
回复
具体怎么写呀
M1CR0S0FT 2008-07-16
  • 打赏
  • 举报
回复
exists & not exists

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧