jeffery_the_wind
jeffery_the_wind

Reputation: 18208

MySQL "NOT IN" not working

Strange thing happening. I am having a problem with my MySQL Community Server 5.1 installed on windows NOT IN query. When I do this query:

select * 
  from table1 
  where date >= "2012-01-01";

returns 582 rows

select * 
  from table1 
  where date >= "2012-01-01" 
    and the_key in (select some_key from table2);

returns 15 rows

so I would expect that the following query would return 582 - 15 = 567 rows

select * 
 from table1 
 where date >= "2012-01-01" 
 and the_key not in (select some_key from table2);

returns 0 rows

Why is this last query not returning any rows?

Upvotes: 20

Views: 19172

Answers (3)

Kibbee
Kibbee

Reputation: 66132

Most likely you have some NULL values in your "key" column. NULL comparisons always return null, which evaluates to false. This can be counter intuitive. For example

SELECT * FROM MyTable WHERE SomeValue <> 0 

Would not return the values with SomeValue = NULL. Even though intuitively, NULL does not equal zero. So to fix the query you have, you should probably do the following.

select * from table1 where date >= "2012-01-01" 
and (key not in (select some_key from table2) OR key IS NULL);

Upvotes: 19

Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36641

Try this.

select * 
 from table1 
 where date >= "2012-01-01" 
 and `key` not in (select some_key from table2 where some_key is not null);

Or using not exists

 select * 
 from table1 
 where date >= "2012-01-01" and not exists ( select some_key from table2 where table2.some_key = table1.key

Upvotes: 33

Sarfraz
Sarfraz

Reputation: 382796

select * 
 from table1 
 where date >= "2012-01-01" 
 and `key` not in (select some_key from table2);

Upvotes: -1

Related Questions