Created
October 19, 2018 16:24
-
-
Save PopocaOrtiz/3a853d5862c18e726e9a2df623f51ecc to your computer and use it in GitHub Desktop.
mysql is null condition using variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set @created_by = null; -- if the var is null, the comparison cant be with '=' | |
select * | |
from users | |
where | |
IF( | |
@created_by is null, users.created_by is null, -- both var and field are null | |
@created_by = users.created_by -- normal comparison, when var is not null | |
) | |
limit 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment