日期:2014-05-20  浏览次数:20777 次

请教 sqlserver 中 not in 在我的代码中为什么失去了作用?
现有两个表   A、B   ,且都有相同的字段   user   ,为varchar   ;
现把属于表A   但不属于表B字段   user   里的值取出来。我写的代码如下:
select   user   from   A,B   where   A.user   not   in   (B.user)。
假设有个值test在表A、B中都有,那么以上语句返回的值不应该有test,但在实际执行中却把它取了出来,请问我错在什么地方呢?

------解决方案--------------------
select A.user from A where A.user not in (SELECT B.user FROM B)
------解决方案--------------------
2、3楼都是正解
------解决方案--------------------
3楼的a.qianshou_username再通过select得到~~~~
------解决方案--------------------
你这样试试看
select DISTINCT c.hy_user from
huiyi_jieshou as c,huiyi_qianshou as a,sys_user as b
where a.qianshou_username <> c.hy_user
--and c.hy_user=b.userno
and c.hy_id= '17 '

为什么会查询出来你不需要的原因是,你有点逻辑混乱, 因为你用的是连表查询. a表的确查询出了你需要的内容,但是你又和c表连接,所以又把c表中你不需要的数据连接进来了