这里有点帮助。我真的不明白如何在 MySQL 中使用这种合并
我已阅读谷歌结果中 how to use coalsece
的第 1 页结果中的所有页面。
我知道它的含义,它返回它遇到的第一个非空值,否则返回 null。
但对我来说还是很模糊。
- 我怎么会看到返回多个值的查询?不只是返回的第一个非空值吗?
- 它如何决定以哪一列为基础?
coalesce(column1,column2)
?如果第一列为空而另一列不为空怎么办? - 或者如果我错了或者我的语法错了,我该如何正确写?
- 有人可以提供一个非常好的简单示例来说明如何使用它吗?
- 以及何时需要使用。
最佳答案
-
How come i saw queries that returns multiple values? isnt it only the first not null value that is returned?
是的,它只是返回的第一个非 NULL 值。对于您认为不是这种情况的查询,您一定是误会了:如果您可以向我们展示一个示例,我们可能能够帮助澄清误解。
-
And how do it decide which column to base? coalesce(column1,column2)? what if first column is null and other column is not null?
按其参数的顺序:在本例中,
column1
在column2
之前。 -
Or if im wrong or my syntax is wrong, how do i properly write it?
你没有错。
-
Can someone provide a very good and simple example on how to use it?
取自 the documentation :
mysql> <strong>SELECT COALESCE(NULL,1);</strong> -> 1 mysql> <strong>SELECT COALESCE(NULL,NULL,NULL);</strong> -> NULL
-
And when it is desirable to use.
希望从列表中选择第一个非 NULL 值时使用。
关于mysql - 如何在 MySQL 中使用 Coalesce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17104329/