1. select * from 表1 结果几行 2。 select * from 表1 where 书号 in (select 书号 from 表2) 结果几行 3。 select * from 表1 where 书号 not in (select 书号 from 表2) 结果?
语法肯定没问题 但最好不用子查询, 用INNER JOIN select A.* from 表1 A INNEER JOIN 表2 B ON A.书号=B.书号 和LEFT JOIN select A.* from 表1 A LEFT JOIN 表2 B ON A.书号=B.书号 WHERE B.书号 IS NULL 效率肯定高
数据库什么版本?两个表的书号数据类型是不是不一样比如一个是char(10),一个是char(12),或者一个是char(10),一个是varchar(10)?试一下: select * from 表1 where rtrim(书号) not in (select rtrim(书号) from 表2)最后,不要用not in ,用not exists
1.not in 没问题,可以这样用效果和not exists一致 2.这2个表的书号字段类型是否完全一致 3.如果一致,那肯定是表1的数据全包括在表2中
select count(*) from 表1 where 书号 in (select 书号 from 表2)select count(*) from 表1是不是一样的??
除非你的 表2 里的书名有 null的值才会这样 这个小bug微软已经知道了
select count(*) from 表1 where 书号 not in (select 书号 from 表2 where 书号 is not null)
--表1-表2 select * from 表1 where not exists(select 1 from 表2 where 表1.书号=表2.书号)
select * from 表1
结果几行
2。
select * from 表1 where 书号 in (select 书号 from 表2)
结果几行
3。
select * from 表1 where 书号 not in (select 书号 from 表2)
结果?
但最好不用子查询,
用INNER JOIN select A.* from 表1 A INNEER JOIN 表2 B ON A.书号=B.书号
和LEFT JOIN
select A.* from 表1 A LEFT JOIN 表2 B ON A.书号=B.书号 WHERE B.书号 IS NULL
效率肯定高
select * from 表1 where rtrim(书号) not in (select rtrim(书号) from 表2)最后,不要用not in ,用not exists
2.这2个表的书号字段类型是否完全一致
3.如果一致,那肯定是表1的数据全包括在表2中
这个小bug微软已经知道了
select * from 表1 where not exists(select 1 from 表2 where 表1.书号=表2.书号)