Why doesn't my query find a NULL value?

This is a discussion on Why doesn't my query find a NULL value? within the MySQL Database forums, part of the Database Forums category; Take a look at this series of queries. > mysql> select * from people where country is null; > Empty ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-07-2008
Patrick Nolan
 
Posts: n/a
Default Why doesn't my query find a NULL value?

Take a look at this series of queries.

> mysql> select * from people where country is null;
> Empty set (0.00 sec)
>
> mysql> select * from people where country = "";
> Empty set (0.01 sec)
>
> mysql> select * from people where country = " ";
> Empty set (0.00 sec)
>
> mysql> select country from people where userid = "murgatroyd";
> +---------+
> | country |
> +---------+
> | NULL |
> +---------+
> 1 row in set (0.00 sec)


This is driving me nuts. Why don't the first three queries find
the record for murgatroyd?

Here's the table definition, abridged to save space:

CREATE TABLE `people` (
`userid` varchar(20) NOT NULL default '',
`country` varchar(15) default NULL, PRIMARY KEY (`userid`),
<etc., etc.>
) ENGINE=InnoDB DEFAULT CHARSET=latin1

It's MySQL version 5.0.45 on Linux.
Reply With Quote
  #2 (permalink)  
Old 07-07-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

Patrick Nolan wrote:
> Take a look at this series of queries.
>
>> mysql> select * from people where country is null;
>> Empty set (0.00 sec)
>>
>> mysql> select * from people where country = "";
>> Empty set (0.01 sec)
>>
>> mysql> select * from people where country = " ";
>> Empty set (0.00 sec)
>>
>> mysql> select country from people where userid = "murgatroyd";
>> +---------+
>> | country |
>> +---------+
>> | NULL |
>> +---------+
>> 1 row in set (0.00 sec)

>
> This is driving me nuts. Why don't the first three queries find
> the record for murgatroyd?
>
> Here's the table definition, abridged to save space:
>
> CREATE TABLE `people` (
> `userid` varchar(20) NOT NULL default '',
> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
> <etc., etc.>
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>
> It's MySQL version 5.0.45 on Linux.
>


What happens if you try:

mysql> select * from people where country = 'NULL';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #3 (permalink)  
Old 07-07-2008
Patrick Nolan
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

On 2008-07-07, Jerry Stuckle <jstucklex@attglobal.net> wrote:
> Patrick Nolan wrote:
>> Take a look at this series of queries.
>>
>>> mysql> select * from people where country is null;
>>> Empty set (0.00 sec)
>>>
>>> mysql> select * from people where country = "";
>>> Empty set (0.01 sec)
>>>
>>> mysql> select * from people where country = " ";
>>> Empty set (0.00 sec)
>>>
>>> mysql> select country from people where userid = "murgatroyd";
>>> +---------+
>>> | country |
>>> +---------+
>>> | NULL |
>>> +---------+
>>> 1 row in set (0.00 sec)

>>
>> This is driving me nuts. Why don't the first three queries find
>> the record for murgatroyd?
>>
>> Here's the table definition, abridged to save space:
>>
>> CREATE TABLE `people` (
>> `userid` varchar(20) NOT NULL default '',
>> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
>> <etc., etc.>
>> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>>
>> It's MySQL version 5.0.45 on Linux.
>>

>
> What happens if you try:
>
> mysql> select * from people where country = 'NULL';
>

Well done. That was the problem. Now I have to figure out
how that happened. Thank you.
Reply With Quote
  #4 (permalink)  
Old 07-08-2008
John Andersen
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

On Jul 7, 10:26 pm, Patrick Nolan <p...@glast2.Stanford.EDU> wrote:
> Take a look at this series of queries.
>
>
>
> > mysql> select * from people where country is null;
> > Empty set (0.00 sec)

>
> > mysql> select * from people where country = "";
> > Empty set (0.01 sec)

>
> > mysql> select * from people where country = " ";
> > Empty set (0.00 sec)

>
> > mysql> select country from people where userid = "murgatroyd";
> > +---------+
> > | country |
> > +---------+
> > | NULL |
> > +---------+
> > 1 row in set (0.00 sec)

>
> This is driving me nuts. Why don't the first three queries find
> the record for murgatroyd?
>
> Here's the table definition, abridged to save space:
>
> CREATE TABLE `people` (
> `userid` varchar(20) NOT NULL default '',
> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
> <etc., etc.>
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>
> It's MySQL version 5.0.45 on Linux.


It looks like your default value is the text value "NULL"! Remove the
default value!
Best wishes,
John, LV
Reply With Quote
  #5 (permalink)  
Old 07-08-2008
John Andersen
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

On Jul 7, 10:26 pm, Patrick Nolan <p...@glast2.Stanford.EDU> wrote:
> Take a look at this series of queries.
>
>
>
> > mysql> select * from people where country is null;
> > Empty set (0.00 sec)

>
> > mysql> select * from people where country = "";
> > Empty set (0.01 sec)

>
> > mysql> select * from people where country = " ";
> > Empty set (0.00 sec)

>
> > mysql> select country from people where userid = "murgatroyd";
> > +---------+
> > | country |
> > +---------+
> > | NULL |
> > +---------+
> > 1 row in set (0.00 sec)

>
> This is driving me nuts. Why don't the first three queries find
> the record for murgatroyd?
>
> Here's the table definition, abridged to save space:
>
> CREATE TABLE `people` (
> `userid` varchar(20) NOT NULL default '',
> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
> <etc., etc.>
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>
> It's MySQL version 5.0.45 on Linux.


It looks to me that your setting of the default value to NULL is
converted into the text value "NULL" thus giving the result!
Remove the default setting for your table!
Best wishes,
John

Ps. Seems my previous post was mislaid :/
Reply With Quote
  #6 (permalink)  
Old 07-08-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

John Andersen wrote:
> On Jul 7, 10:26 pm, Patrick Nolan <p...@glast2.Stanford.EDU> wrote:
>> Take a look at this series of queries.
>>
>>
>>
>>> mysql> select * from people where country is null;
>>> Empty set (0.00 sec)
>>> mysql> select * from people where country = "";
>>> Empty set (0.01 sec)
>>> mysql> select * from people where country = " ";
>>> Empty set (0.00 sec)
>>> mysql> select country from people where userid = "murgatroyd";
>>> +---------+
>>> | country |
>>> +---------+
>>> | NULL |
>>> +---------+
>>> 1 row in set (0.00 sec)

>> This is driving me nuts. Why don't the first three queries find
>> the record for murgatroyd?
>>
>> Here's the table definition, abridged to save space:
>>
>> CREATE TABLE `people` (
>> `userid` varchar(20) NOT NULL default '',
>> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
>> <etc., etc.>
>> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>>
>> It's MySQL version 5.0.45 on Linux.

>
> It looks like your default value is the text value "NULL"! Remove the
> default value!
> Best wishes,
> John, LV
>


No, "default 'NULL'" would be the string 'NULL'. "default NULL" is a
non-existent value.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #7 (permalink)  
Old 07-08-2008
Peter H. Coffin
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

On Tue, 08 Jul 2008 06:02:04 -0400, Jerry Stuckle wrote:
> John Andersen wrote:
>> On Jul 7, 10:26 pm, Patrick Nolan <p...@glast2.Stanford.EDU> wrote:
>>> Take a look at this series of queries.
>>>
>>>
>>>
>>>> mysql> select * from people where country is null;
>>>> Empty set (0.00 sec)
>>>> mysql> select * from people where country = "";
>>>> Empty set (0.01 sec)
>>>> mysql> select * from people where country = " ";
>>>> Empty set (0.00 sec)
>>>> mysql> select country from people where userid = "murgatroyd";
>>>> +---------+
>>>> | country |
>>>> +---------+
>>>> | NULL |
>>>> +---------+
>>>> 1 row in set (0.00 sec)
>>> This is driving me nuts. Why don't the first three queries find
>>> the record for murgatroyd?
>>>
>>> Here's the table definition, abridged to save space:
>>>
>>> CREATE TABLE `people` (
>>> `userid` varchar(20) NOT NULL default '',
>>> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
>>> <etc., etc.>
>>> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>>>
>>> It's MySQL version 5.0.45 on Linux.

>>
>> It looks like your default value is the text value "NULL"! Remove the
>> default value!
>> Best wishes,
>> John, LV
>>

>
> No, "default 'NULL'" would be the string 'NULL'. "default NULL" is a
> non-existent value.


Even if the "abridged" CREATE TABLE is accurate with regard to the
quotes, it doesn't matter in the case where the string 'NULL' came in as
part of an INSERT or UPDATE.

--
Cunningham's First Law:
Any sufficiently complex deterministic system will exhibit
non-deterministic behaviour.
Reply With Quote
  #8 (permalink)  
Old 07-08-2008
Patrick Nolan
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

On 2008-07-08, John Andersen <j.andersen.lv@gmail.com> wrote:
>
> It looks to me that your setting of the default value to NULL is
> converted into the text value "NULL" thus giving the result!
> Remove the default setting for your table!
> Best wishes,
> John
>
> Ps. Seems my previous post was mislaid :/


No. The first response was correct. I had a script which created
an INSERT with "NULL" when I really meant NULL. The default value
doesn't have quotes.
Reply With Quote
  #9 (permalink)  
Old 07-08-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Why doesn't my query find a NULL value?

Peter H. Coffin wrote:
> On Tue, 08 Jul 2008 06:02:04 -0400, Jerry Stuckle wrote:
>> John Andersen wrote:
>>> On Jul 7, 10:26 pm, Patrick Nolan <p...@glast2.Stanford.EDU> wrote:
>>>> Take a look at this series of queries.
>>>>
>>>>
>>>>
>>>>> mysql> select * from people where country is null;
>>>>> Empty set (0.00 sec)
>>>>> mysql> select * from people where country = "";
>>>>> Empty set (0.01 sec)
>>>>> mysql> select * from people where country = " ";
>>>>> Empty set (0.00 sec)
>>>>> mysql> select country from people where userid = "murgatroyd";
>>>>> +---------+
>>>>> | country |
>>>>> +---------+
>>>>> | NULL |
>>>>> +---------+
>>>>> 1 row in set (0.00 sec)
>>>> This is driving me nuts. Why don't the first three queries find
>>>> the record for murgatroyd?
>>>>
>>>> Here's the table definition, abridged to save space:
>>>>
>>>> CREATE TABLE `people` (
>>>> `userid` varchar(20) NOT NULL default '',
>>>> `country` varchar(15) default NULL, PRIMARY KEY (`userid`),
>>>> <etc., etc.>
>>>> ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>>>>
>>>> It's MySQL version 5.0.45 on Linux.
>>> It looks like your default value is the text value "NULL"! Remove the
>>> default value!
>>> Best wishes,
>>> John, LV
>>>

>> No, "default 'NULL'" would be the string 'NULL'. "default NULL" is a
>> non-existent value.

>
> Even if the "abridged" CREATE TABLE is accurate with regard to the
> quotes, it doesn't matter in the case where the string 'NULL' came in as
> part of an INSERT or UPDATE.
>


No argument there, Peter. I was just correcting John's comment
regarding the CREATE TABLE statement.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:27 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0