select not in不行,select in 行

youlostme 2011-01-26 10:30:21
select * from mubiao where item in (select item from producttest)

这条语句,可以查询出来结果。

但是,要是变成了

select * from mubiao where item not in (select item from producttest)

就查不出来任何结果了。

为什么加了not,没有把item中没有的数据查找出来呢?
...全文
629 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
javatemptation 2011-01-27
  • 打赏
  • 举报
回复
因为是三值逻辑,不为true的时候 两种情况 为空 和 为false
xiaoguanzhao 2011-01-27
  • 打赏
  • 举报
回复
是item 的值在mubiao 表中有多条重复的记录吧。。
7964-7486=478条

select item from mubiao group by item having count(*)>1;

看看返回值是否478
xiaoguanzhao 2011-01-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 youlostme 的回复:]
select * from mubiao where item in (select item from producttest)

这条语句,可以查询出来结果。

但是,要是变成了

select * from mubiao where item not in (select item from producttest)

就查不出来任何结果了。

为什么加了not,没有把i……
[/Quote]

是item 的值在mubiao 表中有多条吧。。
7964-7486=478条

select item from mubiao group by item having count(*)>1;

看看返回值是否478
Rotel-刘志东 2011-01-26
  • 打赏
  • 举报
回复
in是有结果的记录,而用了not in与in正好相反没有结果。
叶子 2011-01-26
  • 打赏
  • 举报
回复
没有符合条件的呗
kevin_long 2011-01-26
  • 打赏
  • 举报
回复
4楼 正解
yyfhz 2011-01-26
  • 打赏
  • 举报
回复
反正记录不多,建议LZ把那个In语句的执行结果排序输出到Excel表格中,再把mubiao表的全部记录也采用相同的排序规则并将结果输出到Excel表中,利用Excel的比较能力来检查到底缺了哪些记录
youlostme 2011-01-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zjl8008 的回复:]
是不是有null值影响?isnull()下
[/Quote]

item列没有null值
gw6328 2011-01-26
  • 打赏
  • 举报
回复
说明那个表 不够IN
zjl8008 2011-01-26
  • 打赏
  • 举报
回复
是不是有null值影响?isnull()下
xuam 2011-01-26
  • 打赏
  • 举报
回复

顶!!!!!
[Quote=引用 3 楼 beirut 的回复:]
因为都in了,没有不in的
[/Quote]
feilniu 2011-01-26
  • 打赏
  • 举报
回复
4楼正解。
youlostme 2011-01-26
  • 打赏
  • 举报
回复
不是这样的,刚才忘了说了,我的mubiao 中,有7964条数据。

用select in查询之后,只有7486条数据而已
Shawn 2011-01-26
  • 打赏
  • 举报
回复
CREATE TABLE #temp
(
item INT
)
INSERT #temp
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT NULL

CREATE TABLE #temp1
(
item INT
)
INSERT #temp1
SELECT 1
--自己体会一下吧,注意NULL的情况
select * from #temp where item in (select item from #temp1)
select * from #temp where item not in (select item from #temp1)
黄_瓜 2011-01-26
  • 打赏
  • 举报
回复

因为都in了,没有不in的
GoAwayZ 2011-01-26
  • 打赏
  • 举报
回复
因为你的mubiao 里所有的item都在producttest里能找到啊。
Shawn 2011-01-26
  • 打赏
  • 举报
回复
呵呵,说明你mubiao表中的的item全部在producttest表中.
not in 的意思是:item 不在(select)中
vivai2010 2011-01-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wwwwgou 的回复:]
SQL code
CREATE TABLE #temp
(
item INT
)
INSERT #temp
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT NULL

CREATE TABLE #temp1
(
item INT
)
INSERT #temp1
SELECT 1
--自己体会一下吧,注意NUL……
[/Quote]
+
就是因为null的原因
yubofighting 2011-01-26
  • 打赏
  • 举报
回复
select count(*) from mubiao where isnull(item,0) in (select isnull(item,0)from producttest)

看是否是7964条数据

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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