Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare when value could be both NULL or text

Now I know you can't directly compare NULL to anything (as null is unknown) so how would I achieve the following:

select  *
    from    Material as m
    where   MtrlCode = 826 and
            Exposlimit <> 'compareMe'

Where Exposlimit MAY be NULL or it may not be. 'compareMe' may also be NULL.

Therefore how do I compare the two? Both sides could be either text or NULL.

like image 412
m.edmondson Avatar asked Oct 06 '10 12:10

m.edmondson


2 Answers

select  * 
from    Material as m 
where   MtrlCode = 826 
    and (Exposlimit <> 'compareMe'
         or (Exposlimit is null and compareme is not null) 
         or (Exposlimi is not null and compareme is null))
like image 139
D'Arcy Rittich Avatar answered Sep 19 '22 21:09

D'Arcy Rittich


Use IFNULL function for such cases.

i.e.

WHERE IFNULL(FieldA, 'MagicConstant') = IFNULL(FieldB, 'MagicConstant')

like image 28
Michael Pakhantsov Avatar answered Sep 19 '22 21:09

Michael Pakhantsov