Skip to content

Instantly share code, notes, and snippets.

@PopocaOrtiz
Created October 19, 2018 16:24
Show Gist options
  • Save PopocaOrtiz/3a853d5862c18e726e9a2df623f51ecc to your computer and use it in GitHub Desktop.
Save PopocaOrtiz/3a853d5862c18e726e9a2df623f51ecc to your computer and use it in GitHub Desktop.
mysql is null condition using variables
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