mysql - 如何在 MySQL 中使用 Coalesce

标签 mysql coalesce

这里有点帮助。我真的不明白如何在 MySQL 中使用这种合并

我已阅读谷歌结果中 how to use coalsece 的第 1 页结果中的所有页面。

我知道它的含义,它返回它遇到的第一个非空值,否则返回 null。

但对我来说还是很模糊。

  1. 我怎么会看到返回多个值的查询?不只是返回的第一个非空值吗?
  2. 它如何决定以哪一列为基础? coalesce(column1,column2)?如果第一列为空而另一列不为空怎么办?
  3. 或者如果我错了或者我的语法错了,我该如何正确写?
  4. 有人可以提供一个非常好的简单示例来说明如何使用它吗?
  5. 以及何时需要使用。

最佳答案

  1. How come i saw queries that returns multiple values? isnt it only the first not null value that is returned?

    是的,它只是返回的第一个非 NULL 值。对于您认为不是这种情况的查询,您一定是误会了:如果您可以向我们展示一个示例,我们可能能够帮助澄清误解。

  2. And how do it decide which column to base? coalesce(column1,column2)? what if first column is null and other column is not null?

    按其参数的顺序:在本例中,column1column2 之前。

  3. Or if im wrong or my syntax is wrong, how do i properly write it?

    你没有错。

  4. 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
    
  5. And when it is desirable to use.

    希望从列表中选择第一个非 NULL 值时使用。

关于mysql - 如何在 MySQL 中使用 Coalesce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17104329/

相关文章:

mySQL 返回不应该的结果

ruby - Ruby Sequel 是否与 SQL "coalesce"函数等效?

php - PHP、mySQL 和电子邮件中的字符集

php - 如果 VAR 在 mySQL 数据库中,则返回该行数据作为变量 PHP

php - 具有用户选择的 OPTION 值的 MYSQL SELECT

mysql - 合并 MySQL 中的整个记录

sql - 更改SUM会将NULL返回零

Mysql left join with or condition for fallback 语言

mysql - 仅在更新特定列时更新日期列

php - 做一段时间/循环以获得 10 个随机结果