NULL Safe Equal In MySQL

This operator performs an equiality check just like the = operator does, but it will return 1 instead of NULL if both operands are NULL and will return 0 instead of NULL if one operand is NULL.

Sign for NULL safe operator is <=>

Here are some examples showing the usage of the NULL Safe Operator over simple = operator

Examples:

;Simple SELECT 1=1 ; Return 1 ;NULL Safe SELECT 1<=>1 ; Return 1 ;Simple SELECT NULL=NULL ; Return NULL ;NULL Safe SELECT NULL<=>NULL ; Return 1 ;Simple SELECT 1=NULL ; Return NULL ;NULL Safe SELECT 1<=>NULL ; Return 0
 ;Simple
   SELECT 1=1
   ; Return 1
   
 ;NULL Safe
   SELECT 1<=>1
   ; Return 1
 
 ;Simple
   SELECT NULL=NULL
   ; Return NULL
 ;NULL Safe
   SELECT NULL<=>NULL
   ; Return 1
 
 ;Simple
   SELECT 1=NULL
   ; Return NULL
 ;NULL Safe
   SELECT 1<=>NULL
   ; Return 0

Share This Article