福建正兴台轮胎怎么样:嵌套查询 sql 为什么记录总数不等 如何解决?
来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 20:44:18
select * from zb where 姓名 not in (select distinct 姓名 from jbxx) 得 569条记录
select * from zb where 姓名 in (select distinct 姓名 from jbxx)
得 733条记录
569+733=1302条记录
但是zb 的记录总数 只有1287条按照道理两个总数应相等啊(zb表中的姓名有重复,jbxx 表中的姓名也有重复)?百思不得其解
请高手帮忙,谢谢!
其实不管加不加 distinct 结果都一样
select * from zb where 姓名 not in (select 姓名 from jbxx) 得 569条记录
select * from zb where 姓名 in (select 姓名 from jbxx)
得 733条记录
569+733=1302条记录
jbxx表1614条记录
谢谢我看了一下第一个查询怎么会有两条应当是0条啊
not in 表示zb当中的姓名在
select distinct 姓名 from jbxx也就是A和B中找不到的意思吗?
select * from zb where 姓名 in (select distinct 姓名 from jbxx)
得 733条记录
569+733=1302条记录
但是zb 的记录总数 只有1287条按照道理两个总数应相等啊(zb表中的姓名有重复,jbxx 表中的姓名也有重复)?百思不得其解
请高手帮忙,谢谢!
其实不管加不加 distinct 结果都一样
select * from zb where 姓名 not in (select 姓名 from jbxx) 得 569条记录
select * from zb where 姓名 in (select 姓名 from jbxx)
得 733条记录
569+733=1302条记录
jbxx表1614条记录
谢谢我看了一下第一个查询怎么会有两条应当是0条啊
not in 表示zb当中的姓名在
select distinct 姓名 from jbxx也就是A和B中找不到的意思吗?
晕,两个表。。。看错了。。
^_^,我知道了,主要是因为zb有重复值的原因。。
举例:
zb jbxx
A A
A B
B B
B B
你仔细分析下看看,。
select * from zb where 姓名 not in (select distinct 姓名 from jbxx) 得:2条记录
select * from zb where 姓名 in (select distinct 姓名 from jbxx) 得:4条记录
2+4=6条记录
zb只有四条记录。。。。
---------------------
select * from zb where 姓名 not in (select distinct 姓名 from jbxx) 得:2条记录
解剖一下:
select distinct 姓名 from jbxx 得到: A 和B
not in (select distinct 姓名 from jbxx) 得到: B 和B
select * from zb where 姓名 not in (select distinct 姓名 from jbxx) 得:zb中的B和B
一直都没碰到过这种问题
不过我试想了一下
最大的可能就是zb表中有空姓名的记录
而jbxx中没有空姓名的记录
cql5780,zhangkunshuai,你们都说错了!第一条语句怎么可能有两条呢?明显是0条嘛!
我赞同 jiushixuefeng的说法,很可能是有空名字的!要不然查出来肯定相等的!