使用 not exists, not in 须小心

来源:互联网 发布:天狗街源码交易 编辑:程序博客网 时间:2024/05/16 19:56

not exists 和 not in 对空值返回结果不同.

product_type_id 为空时,not exists 有返回,而not in没有返回值。

NOT EXISTS:

SELECT product_type_id, nameFROM product_types outerWHERE NOT EXISTS  (SELECT 1   FROM products inner   WHERE inner.product_type_id = outer.product_type_id);
查询结果:

PRODUCT_TYPE_ID NAME--------------- ----------              5 Magazine

NOT IN:

SELECT product_type_id, nameFROM product_typesWHERE product_type_id NOT IN  (SELECT product_type_id   FROM products);


查询结果 :no rows selected

原创粉丝点击