当前位置: 代码迷 >> Sql Server >> 连接查询有关问题!
  详细解决方案

连接查询有关问题!

热度:2   发布时间:2016-04-27 13:02:13.0
连接查询问题!!!急,在线等
有两个表:表1和表2
表1 表2
ID UserNo UserName ID UserNo  
1 01 hgc 1 01  
2 01 gqx 2 02 
3 02 ht  
4 03 xs

我想得到表1中的UserNo未出现在表2的记录
即理想结果应该为1条:
UserNo UserName
 03 xs

我是这样写SQl的: select e.UserNo,e.UserName from 表1 e,表2 d where e.UserNO<>d.UserNo;
得到的结果是:
01 hgc
01 gqx
02 ht
03 xs
03 xs

请问该SQL怎么写才能得到想要的结果?

------解决方案--------------------
SQL code
select * from 表1 a where not exists(select 1 from 表2 where userno=a.userno)
------解决方案--------------------
SQL code
select * from 表1 e where not exists (select 1 from 表2 d where e.UserNO=d.UserNo
------解决方案--------------------
SQL code
select * from t1 where userno not in(select userno from t2)
------解决方案--------------------
select * from 表1 a where not exists(select 1 from 表2 b where a.UserNo =b.UserNo )
  相关解决方案