求两条sql语句...

伊凡yifan 2007-12-26 10:28:36
两个表结构一模一样如下:

A表 LoverA B表loverB

两表字段相同:id int
phone varchar
time datetime
is_valid bit

两个表的数据不同,如LOVERA 有100条记录 LOVERB有200条记录

用一条sql语句 根据phone 列出两个表不相同的记录集合

用一条sql语句 根据phone 统计两个表的不相同的记录集的总数
...全文
44 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pt1314917 2007-12-26
  • 打赏
  • 举报
回复

列出A表中不在B表中的记录
select * from LoverA a where not exists(select 1 from LoverB where id=a.id and phone=a.phone
and time=a.time and is_valid=a.is_valid)

列出A表中不在B表中的记录的总数:
select count(1) from LoverA a where not exists(select 1 from LoverB where id=a.id and phone=a.phone and time=a.time and is_valid=a.is_valid)

列出B表中不在A表中的记录:
select * from LoverB a where not exists(select 1 from LoverA where id=a.id and phone=a.phone
and time=a.time and is_valid=a.is_valid)

列出B表中不在A表中的记录的总数:
select count(1) from LoverB a where not exists(select 1 from LoverA where id=a.id and phone=a.phone and time=a.time and is_valid=a.is_valid)


dawugui 2007-12-26
  • 打赏
  • 举报
回复
--不知道你到底需要哪个?
select a.* from lovera a where phone not in (select phone from loverb)
select b.* from loverb b where phone not in (select phone from lovera)

select count(*) from lovera a where phone not in (select phone from loverb)
select count(*) from loverb b where phone not in (select phone from lovera)

select m.cnt + n.cnt from
(select cnt = count(*) from lovera a where phone not in (select phone from loverb)) m,
(select cnt = count(*) from loverb b where phone not in (select phone from lovera)) n


34,593

社区成员

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

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