select * from shopopened where productid not in (select productid from paper)两个表中的数据在上万条,shopopened表和paper是多对一的关系。 为什么上面的语句查询是空的(确认shopopened表中有paper表中不存在的值)
select * from shopopened where productid not in (select productid from paper where productid is not null )
这个问题在其他的数据库里也会有,是因为 not in的处理有问题。比如:select * from shopopened where productid not in (1,2,null )那么就没有记录返回,因为 里面有null值,而null什么都不 当用productid和null比较的时候,会返回false,所以一条记录都不会返回,所以要改成:select * from shopopened where productid not in (select productid from paper where productid is not null )
select * from shopopened a
left join paper b on
a.productid=b.productid where b.productid is null
先问一下两个表是否存在关联,即shopopened 中的productid 是否为外键????
这个问题在其他的数据库里也会有,是因为 not in的处理有问题。比如:select * from shopopened where productid not in (1,2,null )那么就没有记录返回,因为 里面有null值,而null什么都不 当用productid和null比较的时候,会返回false,所以一条记录都不会返回,所以要改成:select * from shopopened where productid not in (select productid from paper where productid is not null )