The expression IFNULL(CL.EmploymentType, 3)
basically means: if CL.EmploymentType IS NULL
then use 3
instead. The original value of CL.EmploymentType
is used if it is not NULL
.
If I understand correctly your question, you need to select the rows having NULL
or 3
in the column CL.EmploymentType
.
The query is:
SELECT (@v:=2) AS Row, CL.LeaveTypeId, CL.NumberOfLeave
FROM `CompanyLeave` CL
WHERE IFNULL(CL.EmploymentType, 3) = 3;
Update:
If only one row must be returned (the one having 3
being preferred over those having NULL
) then the rows must be sorted using a criteria that puts the NOT NULL
value in front and a LIMIT 1
clause must be added.
MySQL documentation about NULL
says:
When doing an ORDER BY
, NULL
values are presented first if you do ORDER BY ... ASC
and last if you do ORDER BY ... DESC
.
The updated query is:
SELECT (@v:=2) AS Row, CL.LeaveTypeId, CL.NumberOfLeave
FROM `CompanyLeave` CL
WHERE IFNULL(CL.EmploymentType, 3) = 3;
ORDER BY CL.EmploymentType DESC
LIMIT 1