我有两张表,表1(原表)如下:
账号: 姓名:
123456 张三
111122 李四
124556 王五
...表2:
账号: 姓名:
123456 张三
111122 李六
124556 王五
...现在我要通过个表1(原表)来核对表2的中记录是否正确,并将正确记录选出来,在核对时候要求同
时核对账号和姓名,也就是说不仅要求账号正确,还要求该账号所对应的姓名也必须相符,比如第二条记录在
表2中的是
111122 李六
虽然在表1中账号也是111122 ,但是表1中的姓名是李四,而表2中的姓名是李六,这条记录就不符全条件,
如果只核对账号是否正解我会写,但是同是核对账号我姓名就不会了,请大家指导一下。
账号: 姓名:
123456 张三
111122 李四
124556 王五
...表2:
账号: 姓名:
123456 张三
111122 李六
124556 王五
...现在我要通过个表1(原表)来核对表2的中记录是否正确,并将正确记录选出来,在核对时候要求同
时核对账号和姓名,也就是说不仅要求账号正确,还要求该账号所对应的姓名也必须相符,比如第二条记录在
表2中的是
111122 李六
虽然在表1中账号也是111122 ,但是表1中的姓名是李四,而表2中的姓名是李六,这条记录就不符全条件,
如果只核对账号是否正解我会写,但是同是核对账号我姓名就不会了,请大家指导一下。
--适用于MS SQL SERVER。
下面的过滤出正确的:
select * from biao2 a where exists ( select 1 from biao1 where 账号=a.账号 and 姓名=a.姓名)
where not exists(select * from 表1
where 表1.账号=表2.账号 and 表1.名称=表2.名称)
select b.* from zh a join ys b on a.zh=b.zh and a.xm=b.xm 可以选出正确的记录,现在有一个问题我要选出账号不正确,或者账号正确但是姓名不正确的记录,我用如下命令:
select * from ys where zh not in(select b.zh from zh a join ys b on a.zh=b.zh and a.xm=b.xm)得出提结果是只能选出账号不正确的记录,账号正确但是姓名不正确的记录选不出来,请问一下
该怎样写,如果写成如下:
select * from ys where not exists(select b.* from zh a join ys b on a.zh=b.zh and a.xm=b.xm)
执行时没有错误,但执行时好像死机了,出不来.
select [账号], [姓名] from [表2] minus (select b.[账号], b.[姓名] from [表1] a join [表2] b on a.[账号]=b.[账号] and a.[姓名]=b.[姓名])
select * from ys where zh not in(select b.zh from zh a join ys b on a.zh=b.zh and a.xm=b.xm)
union all
select * from ys where xm not in(select b.xm from zh a join ys b on a.zh=b.zh and a.xm=b.xm)
选出正确的账号:
select b.* from zh a join ys b on a.zh=b.zh and a.xm=b.xm
选出错误的账号:
select * from ys where zh not in(select b.zh from zh a join ys b on a.zh=b.zh and a.xm=b.xm)
之前在用第二条语句是出问题,不是这条语句不正确,而是我的一个表的被破坏了,所以不正确,现在把表搞好
了,以上语句就都正确了,感谢以上给我帮助的老师,谢谢。