MySQL mysql中的ifnull和coalesce有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18528468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference bewteen ifnull and coalesce in mysql?
提问by shree18
select ifnull(null,'replaces the null')
-->replaces the null
select coalesce(null,null,'replaces the null')
-->replaces the null
In both clauses the main difference is argument passing. For ifnull
it's two parameter and coalesce 2 or 3 we can pass,except that do we have any other difference between these two?
And how it differs in MSSql.
在这两个子句中,主要区别在于参数传递。对于ifnull
它的两个参数和合并 2 或 3 我们可以传递,除了这两者之间还有其他区别吗?以及它在 MSSql 中的不同之处。
回答by Aleks G
The main difference between the two is that IFNULL
function takes two arguments and returns the first one if it's not NULL
or the second if the first one is NULL
.
两者之间的主要区别是,IFNULL
函数有两个参数,返回的第一个,如果它不是NULL
,或者如果第一个是第二NULL
。
COALESCE
function can take two or more parameters and returns the first non-NULL parameter, or NULL
if all parameters are null, for example:
COALESCE
函数可以接受两个或多个参数并返回第一个非 NULL 参数,或者NULL
如果所有参数都为 null,例如:
SELECT IFNULL('some value', 'some other value');
-> returns 'some value'
SELECT IFNULL(NULL,'some other value');
-> returns 'some other value'
SELECT COALESCE(NULL, 'some other value');
-> returns 'some other value' - equivalent of the IFNULL function
SELECT COALESCE(NULL, 'some value', 'some other value');
-> returns 'some value'
SELECT COALESCE(NULL, NULL, NULL, NULL, 'first non-null value');
-> returns 'first non-null value'
UPDATE:MSSQL does stricter type and parameter checking. Further, it doesn't have IFNULL
function but instead ISNULL
function, which needs to know the types of the arguments. Therefore:
更新:MSSQL 进行更严格的类型和参数检查。此外,它没有IFNULL
函数,而是ISNULL
函数,它需要知道参数的类型。所以:
SELECT ISNULL(NULL, NULL);
-> results in an error
SELECT ISNULL(NULL, CAST(NULL as VARCHAR));
-> returns NULL
Also COALESCE
function in MSSQL requires at least one parameter to be non-null, therefore:
COALESCE
MSSQL 中的函数也要求至少一个参数为非空,因此:
SELECT COALESCE(NULL, NULL, NULL, NULL, NULL);
-> results in an error
SELECT COALESCE(NULL, NULL, NULL, NULL, 'first non-null value');
-> returns 'first non-null value'
回答by Sasha
Pros of COALESCE
的优点 COALESCE
COALESCE
is SQL-standard function.While
IFNULL
is MySQL-specific and its equivalent in MSSQL (ISNULL
) is MSSQL-specific.COALESCE
can work with two or more arguments(in fact, it can work with a single argument, but is pretty useless in this case:COALESCE(a)
≡a
).While MySQL's
IFNULL
and MSSQL'sISNULL
are limited versions ofCOALESCE
that can work with two arguments only.
COALESCE
是 SQL 标准函数。虽然
IFNULL
是特定于 MySQL 的,它在 MSSQL 中的等价物 (ISNULL
) 是特定于 MSSQL 的。COALESCE
可以使用两个或多个参数(实际上,它可以使用单个参数,但在这种情况下非常无用:COALESCE(a)
≡a
)。虽然 MySQL
IFNULL
和 MSSQLISNULL
是有限版本,COALESCE
但只能使用两个参数。
Cons of COALESCE
的缺点 COALESCE
Per Transact SQL documentation,
COALESCE
is just a syntax sugar forCASE
and can evaluate its arguments more that once. In more detail:COALESCE(a1, a2, …, aN)
≡CASE WHEN (a1 IS NOT NULL) THEN a1 WHEN (a2 IS NOT NULL) THEN a2 ELSE aN END
. This greatly reduces the usefulness ofCOALESCE
in MSSQL.On the other hand,
ISNULL
in MSSQL is a normal function and never evaluates its arguments more than once.COALESCE
in MySQL and PostgreSQL neither evaluates its arguments more than once.At this point of time, I don't know how exactly SQL-standards define
COALESCE
.As we see from previous point, actual implementations in RDBMS vary: some (e.g. MSSQL) make
COALESCE
to evaluate its arguments more than once, some (e.g. MySQL, PostgreSQL) — don't.c-treeACE, which claims it's
COALESCE
implementation is SQL-92 compatible, says: "This function is not allowed in a GROUP BY clause. Arguments to this function cannot be query expressions." I don't know whether these restrictions are really within SQL-standard; most actual implementations ofCOALESCE
(e.g. MySQL, PostgreSQL) don't have such restrictions.IFNULL
/ISNULL
, as normal functions, don't have such restrictions either.
每个Transact SQL 文档,
COALESCE
只是语法糖CASE
,可以多次评估其参数。更详细地说:COALESCE(a1, a2, …, aN)
≡CASE WHEN (a1 IS NOT NULL) THEN a1 WHEN (a2 IS NOT NULL) THEN a2 ELSE aN END
. 这大大降低了COALESCE
在 MSSQL 中的用处。另一方面,
ISNULL
在 MSSQL 中是一个普通函数,并且不会多次评估其参数。COALESCE
在 MySQL 和 PostgreSQL 中,不会多次评估其参数。目前,我不知道 SQL 标准是如何定义
COALESCE
.正如我们从前一点所看到的,RDBMS 中的实际实现各不相同:有些(例如 MSSQL)会
COALESCE
多次评估其参数,有些(例如 MySQL、PostgreSQL)则不会。声称其
COALESCE
实现与 SQL-92 兼容的c-treeACE说:“在 GROUP BY 子句中不允许使用此函数。此函数的参数不能是查询表达式。” 我不知道这些限制是否真的符合 SQL 标准;大多数实际实现COALESCE
(例如 MySQL、PostgreSQL)没有这样的限制。IFNULL
/ISNULL
,作为普通函数,也没有这样的限制。
Resume
恢复
Unless you face specific restrictions of COALESCE
in specific RDBMS, I'd recommend to always use COALESCE
as more standard and more generic.
除非您COALESCE
在特定 RDBMS 中面临特定限制,否则我建议始终使用COALESCE
更标准和更通用的方式。
The exceptions are:
例外情况是:
- Long-calculated expressions or expressions with side effects in MSSQL (as, per documentation,
COALESCE(expr1, …)
may evaluateexpr1
twice). - Usage within
GROUP BY
or with query expressions in c-treeACE. - Etc.
- MSSQL 中长期计算的表达式或具有副作用的表达式(根据文档,
COALESCE(expr1, …)
可能会计算expr1
两次)。 GROUP BY
在 c-treeACE 中的查询表达式内或与查询表达式一起使用。- 等等。
回答by praba
Differences in SQL-Server:
SQL-Server 的差异:
There is no
IFNULL()
function but a similarISNULL()
ISNULL
takes only 2 parameters whereas COALESCE takes variable number of parametersCOALESCE
is based on the ANSI SQL standard whereasISNULL
is a proprietary TSQL functionValidations for
ISNULL
andCOALESCE
is also different. For example,NULL
value forISNULL
is converted to int, whereas forCOAELSCE
you have to provide a type. Ex:ISNULL(NULL,NULL)
: is int.COALESCE(NULL,NULL)
: will throw an error.COALESCE(CAST(NULL as int),NULL)
: is valid and returns int.
Data type determination of the resulting expression –
ISNULL
uses the first parameter type,COALESCE
follows theCASE
expression rules and returns type of value with highest precedence.
没有
IFNULL()
功能,但类似ISNULL()
ISNULL
只需要 2 个参数,而 COALESCE 需要可变数量的参数COALESCE
基于 ANSI SQL 标准,ISNULL
而是专有的 TSQL 函数为验证
ISNULL
和COALESCE
也不同。例如,NULL
值 forISNULL
被转换为 int,而 forCOAELSCE
你必须提供一个类型。前任:ISNULL(NULL,NULL)
: 是整数。COALESCE(NULL,NULL)
: 会报错。COALESCE(CAST(NULL as int),NULL)
: 有效并返回 int。
结果表达式的数据类型确定 -
ISNULL
使用第一个参数类型,COALESCE
遵循CASE
表达式规则并返回具有最高优先级的值类型。
回答by jboi
ifnull
can only replace a null value of the first parameter. Whereas coalesce
can replace any value with another value. With coalesce
in standard SQL you can have many parameters transforming many values.
ifnull
只能替换第一个参数的空值。而coalesce
可以用另一个值替换任何值。随着coalesce
标准的SQL,你可以有很多的参数改变了许多价值。
EDIT the example according to comments below.
根据下面的评论编辑示例。
Example: coalesce(null, null, null, 'b*', null, 'null*')
returns 'b*' and it is not possible to do with ifnull
.
示例:coalesce(null, null, null, 'b*', null, 'null*')
返回 'b*' 并且无法使用ifnull
.
回答by user8546027
This db2 SQL will not work with COALESE, I will not see any rows retrieved. Since I used IFNULL it is working as expected
这个 db2 SQL 不能与 COALESE 一起工作,我不会看到任何检索到的行。由于我使用了 IFNULL 它按预期工作
select a.mbitno ,a.mbstqt,ifnull(b.apr,0)
from
(
select mmstcd,mbstat,mbfaci,mbwhlo,mbitno,mbstqt,MBALQT from libl.mitbal inner join libl.mitmas on
mmcono=mbcono and mmitno=mbitno
where mbcono=200 and mbstat in ('20','50') and mmstcd>0
)
as a left join
(
select mlfaci,mlwhlo,mlitno,mlstas,sum(mlstqt) as APR from libl.mitloc where mlcono=200 and mlstas='2'
group by mlfaci,mlwhlo,mlitno,mlstas
)
b on b.mlfaci=a.mbfaci and b.mlwhlo=a.mbwhlo and b.mlitno=a.mbitno
where a.mbitno in 'GWF0240XPEC' and a.mbstqt>0 and a.mbstqt<>ifnull(b.apr,0)