Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

not with "is not null" inconsistent?

Status
Not open for further replies.

russellr

Programmer
Sep 11, 2001
7
AU
Am I missing something obvious, or is this bug...
Code:
    (data = 15 and data is not null)
    not (data = 15 and data is not null)
should be opposites.

However, the following SQL script shows this not to be the case:
Code:
        drop table if exists bugtest;
        create table bugtest (id int not null auto_increment primary key,data smallint);
        insert into bugtest values (NULL,10);
        insert into bugtest values (NULL,20);
        insert into bugtest values (NULL,30);
        insert into bugtest values (NULL,40);
        insert into bugtest values (NULL,50);
        insert into bugtest values (NULL,NULL);

        select * from bugtest;

        select count(*) from bugtest where (data = 15 and data is not null);
        select count(*) from bugtest where not (data = 15 and data is not null);
        select count(*) from bugtest where (data <> 15 or data is null);
        drop table bugtest;
The second select should return &quot;6&quot; not &quot;5&quot;.

Note that the third select is logically identical to the second select according to DeMorgan's Law. And it returns &quot;6&quot;, correctly.
 
maybe this is the prob:

Logical NOT. Returns 1 if the argument is 0, otherwise returns 0. Exception: NOT NULL returns NULL
 
russelr:

I agree with you. It seems like Mysql doesn't handle three valued logic properly.
 
What results happen when you have the following

select count(*) from bugtest where (data != 15 and data is null);
 
Thanks for the replies.

To answer &quot;likelylad&quot;, this query:
Code:
    select count(*) from bugtest where (data <> 15 and data is null)
returns 0. Which is correct, because &quot;NULL <> 15&quot; is FALSE.

Here's how to write query 2, so it works!!!
Code:
    select count(*) from bugtest where not (data is not null 
and data = 15)
In other words, just reverse the AND clause.

FYI, here's the final test script, and its output:
Code:
    drop table if exists bugtest;
    create table bugtest (id int not null auto_increment primary key,data smallint);
    insert into bugtest values (NULL,10);
    insert into bugtest values (NULL,20);
    insert into bugtest values (NULL,30);
    insert into bugtest values (NULL,40);
    insert into bugtest values (NULL,50);
    insert into bugtest values (NULL,NULL);

    select * from bugtest;

    select count(*) from bugtest where (data = 15 and data is not null);
    select count(*) from bugtest where not (data = 15 and data is not null);
    select count(*) from bugtest where not (data is not null and data = 15);
    select count(*) from bugtest where (data <> 15 or data is null);
    select count(*) from bugtest where (data <> 15 and data is null);
    drop table bugtest;

OUTPUT:

Code:
--------------
drop table if exists bugtest
--------------
--------------
create table bugtest (id int not null auto_increment primary key,data smallint)
--------------
--------------
insert into bugtest values (NULL,10)
--------------
--------------
insert into bugtest values (NULL,20)
--------------
--------------
insert into bugtest values (NULL,30)
--------------
--------------
insert into bugtest values (NULL,40)
--------------
--------------
insert into bugtest values (NULL,50)
--------------
--------------
insert into bugtest values (NULL,NULL)
--------------
--------------
select * from bugtest
--------------
id      data
1       10
2       20
3       30
4       40
5       50
6       NULL
--------------
select count(*) from bugtest where (data = 15 and data is not null)
--------------
count(*)
0
--------------
select count(*) from bugtest where not (data = 15 and data is not null)
--------------
count(*)
5
--------------
select count(*) from bugtest where not (data is not null and data = 15)
--------------
count(*)
6
--------------
select count(*) from bugtest where (data <> 15 or data is null)
--------------
count(*)
6
--------------
select count(*) from bugtest where (data <> 15 and data is null)
--------------
count(*)
0
--------------
drop table bugtest
--------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top