简体   繁体   English

我如何在Where子句中使用IFNULL

[英]How can i use IFNULL in Where clause

I want to use IFNULL() in such a way that I can select the record containing NULL or, if a value is present, then select the record matchinga particular value. 我想以这样的方式使用IFNULL() ,我可以选择包含NULL的记录,或者如果存在值,则选择匹配特定值的记录。

My query is: 我的查询是:

SELECT (@v:=2) AS Row, CL.LeaveTypeId, CL.NumberOfLeave
FROM `CompanyLeave` CL
WHERE(IFNULL(CL.EmploymentType,0)=3);

column EmploymentType can contain either an Integer or NULL . columns EmploymentType可以包含IntegerNULL

I want to select the record matching the specified value, or, if none matches, then the record containing NULL . 我想选择匹配指定值的记录,或者,如果没有匹配,则选择包含NULL的记录。

I am interpreting the question as a prioritization. 我将这个问题解释为优先顺序。 If a record with 3 exists, choose that. 如果存在3的记录,请选择该记录。 Otherwise, choose the one that is NULL , if it exists. 否则,选择NULL (如果存在)。

If so, this might do what you want: 如果是这样,这可能会做你想要的:

SELECT (@v:=2) AS Row, CL.LeaveTypeId, CL.NumberOfLeave
FROM `CompanyLeave` CL
WHERE CL.EmployementType = 3 or CL.EmployementType IS NULL
ORDER BY (CL.EmployementType = 3) DESC
LIMIT 1;

This will return the row with 3 , if present. 如果存在,这将返回3行。 Otherwise, it will return a row with NULL , if one exists. 否则,它将返回一个NULL行,如果存在的话。

The expression IFNULL(CL.EmploymentType, 3) basically means: if CL.EmploymentType IS NULL then use 3 instead. 表达式IFNULL(CL.EmploymentType, 3)基本上意味着:如果CL.EmploymentType IS NULL则使用3代替。 The original value of CL.EmploymentType is used if it is not NULL . 如果不是NULL则使用CL.EmploymentType的原始值。

If I understand correctly your question, you need to select the rows having NULL or 3 in the column CL.EmploymentType . 如果我正确理解了您的问题,则需要在CL.EmploymentType列中选择NULL3的行。
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. 如果只返回一行(具有3那一行优先于那些具有NULL那一行),那么必须使用将NOT NULL值放在前面并且必须添加LIMIT 1子句的条件对行进行排序。

MySQL documentation about NULL says: 关于NULL MySQL 文档说:

When doing an ORDER BY , NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC . 在执行ORDER BY ,如果您执行ORDER BY ... ASC ,则首先显示NULL值,如果执行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

You can use IF statement instead of IFNULL() 您可以使用IF语句而不是IFNULL()

IF(condition, expres1, expres2) 

It means that if condition is satisfied then return expres1 else expres2 这意味着如果条件满足则返回expres1 else expres2

SELECT (@v:=2) AS Row, CL.LeaveTypeId, CL.NumberOfLeave
FROM `CompanyLeave` CL
WHERE IF(CL.EmploymentType IS NULL, 0, CL.EmploymentType) = 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM