CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   difference between 'is null' and '= null' (http://www.codingforums.com/showthread.php?t=288251)

BubikolRamios 02-25-2013 11:47 AM

difference between 'is null' and '= null'
 
Both works, with different result. What is the logic ?

Celtboy 02-25-2013 04:53 PM

You'll want to use IS NULL. = NULL is an arithmetic operator that can have some odd results:

http://dev.mysql.com/doc/refman/5.0/...with-null.html

BubikolRamios 02-25-2013 05:42 PM

Yes, I was convinced, until special case query(where I got a bit confused (-:) that '= null' does not exists at all.

But see this dilema:
tab1
Code:

id     
1
2

tab2
Code:

id      varcharfiield
1      null

I would say here in condition :

select * from tab2 where varcharfield = null'

and in this case:

select * from tab1 left join tab2 on tab1.id = tab2.id were tab1.id is null

coz, in first case null is actualy there, in table, in second it is not, it is produced by query.

Do you mean 'is null' should work in any upper case & in any case at all ?

felgall 02-25-2013 06:14 PM

In databases NULL means the value is unknown and therefore NULL == NULL is false since two unknowns are rarely going to be the same value if you ever find out what they are. So testing for = NULL will never find a match as NULL is never equal to anything.

Once you move out of the database null has a completely different meaning and two fields set to null are equal. The NULL in the database doesn't extract as a meaningful value though because outside the database there is no corresponding value meaning unknown to give the field.

Old Pedant 02-25-2013 07:07 PM

Quote:

Originally Posted by felgall (Post 1315842)
Once you move out of the database null has a completely different meaning and two fields set to null are equal.

NOT TRUE AT ALL!

That may be true in PHP and JavaScript, but it is *NOT* true in some computer languages.

This is a table of the comparison operators from VBScript, just for example:
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx

VB and VB.NET are essentially the same: They use the "IS" operator for testing object equality, and two nulls will not match using anything except the IS operator. Pascal, in most variations I've seen, is the same.

Only languages that don't seem to have a built in "isNull" or equivalent function/operator seem to treat a pair of nulls as being equal. It *IS* misleading to say that a pair of nulls are equal, for the reason Felgall gave: NULL means "unknown" and how can you know that a pair of unknowns are equal. I think this is a case where VB/VB.NET/VBScript do it better than JavaScript, C, C++, et al. If nothing else, how does it make sense to ask
Code:

    if ( a <= null )
or
    if  ( b > null )

??? Clearly, those are nonsense questions. But stupid JavaScript, in these conditions, treats null the same as zero. UGH! I think that's uglier than pig snot.

felgall 02-25-2013 08:44 PM

Quote:

Originally Posted by Old Pedant (Post 1315852)
NOT TRUE AT ALL!

That may be true in PHP and JavaScript, but it is *NOT* true in some computer languages.

I should have said CAN HAVE rather than HAS. (Just about all the languages I know treat null as meaning something completely different from what it means in any other language).

Quote:

Originally Posted by Old Pedant (Post 1315852)
But stupid JavaScript, in these conditions, treats null the same as zero.

That's why you should always use === and !== in comparisons in JavaScript. Null in JavaScript is a placeholder for an object that has not yet been created and so two placeholders can be considered to have the same value hence null === null in JavaScript but comparing to anything else using the correct comparison operators will return false.

In JavaScript (null === 0) will return false because it does not treat them the same unless you are sloppy and use == which first tries to convert both values to the same type and both null and 0 can be converted to false giving false == false which is true..

Old Pedant 02-25-2013 10:18 PM

YES! I think I wish that JS disallowed using null with == and !=. I certainly admit to sloppily using them way too often.

I *REALLY* dislike code that does stuff such as
Code:

if ( ! document.getElementsByClassName ) { ... }
because it implies that a method reference is a boolean value. I think you should *HAVE* to write
Code:

if ( document.getElementsByClassName === null ) { ... }
Ah, well.

felgall 02-26-2013 01:24 AM

Quote:

Originally Posted by Old Pedant (Post 1315914)
I think you should *HAVE* to write
Code:

if ( document.getElementsByClassName === null ) { ... }

There is an error in your logic there. The mention of document.getElementsByClassName would presumably create it if it doesn't exist. As it now exists without having had a value allocated it now has a value of undefined and not null. When using === undefined !== null.

The correct way to write it is:

Code:

if (typeof document.getElementsByClassName === undefined ) { ... }

Old Pedant 02-26-2013 02:36 AM

SORRY! Senior moment. I knew that.


All times are GMT +1. The time now is 11:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.