mysql - How does NOT IN subquery work with NULL values? -


i confused how following works in mysql. in queries below, first select returns rows table2 while second select returns none of rows. there explanation of how null works not in operator. there documentation explains this?

create table table1 (    id int unsigned not null auto_increment,    primary key (id) );  create table table2 (    id int unsigned not null auto_increment,    table1_id int unsigned,    primary key (id) );  insert table2 (id, table1_id) values (1, null);  select count(*) table2 table1_id not in (select id table1); +----------+ | count(*) | +----------+ |        1 | +----------+  insert table1 (id) values (1);  select count(*) table2 table1_id not in (select id table1); +----------+ | count(*) | +----------+ |        0 | +----------+ 

the reason according sql specification, foo in(a,b,c) translates ( foo = or foo = b or foo = c ). thus, if have foo in(null, 1, 2) foo = null or foo = 1 or foo = 2. since foo = null unknown , evaluated false purposes of filtering, nulls in in expression return no results.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -