SQL中not in 查不出数据

select uid, `name`   from cars_info_collection_new where `code` not in (select mc_id from car_original_category );

用这句话一直查不出数据,

select uid, `name` from cars_info_collection_new where not exists (select mc_id from car_original_category where mc_id=`code`);

而用not exists可以查出数据。

查了好多资料,原来是因为嵌套查询中(也就是括号中的查询结果存在为null的数据),这种情况下用in会返回

null,可以改用exists。

in和exists  区别:

in :把外表和内表作hash 连接

Exists:对外表作loop循环,每次loop循环再对内表进行查询。

猜你喜欢

转载自blog.csdn.net/wang_nian/article/details/107794686
今日推荐