Mysql将空值修改为0

标签 mysql

我的 mysql 数据库有这条 SQL 语句。

SELECT MAX(customer_ID) AS high_customer_ID FROM `customers`.`customers` WHERE our_customer_ID=1;

但是当没有 customer_ID 且 our_customer_ID = 1 时,语句返回 null 但我希望它返回 0 我该怎么做。

最佳答案

您可以使用 coalescenull 的情况下分配默认值的函数:

SELECT COALESCE(MAX(customer_ID), 0) AS high_customer_ID 
FROM   `customers`.`customers`
WHERE  our_customer_id = 1;

关于Mysql将空值修改为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34966971/

相关文章:

php - 如何检测数据库结构是否发生变化? (不是内容!)

mysql - 将 Percona 集群从 5.6 更新到 5.7

GROUP BY 和 JOIN 的 MySQL 性能

php - 多维数组输出

MySQL/正则表达式 : Partial regexp match

mysql - ansible中mysql插入/更新请求的转义字符串值

mysql - 取消默认数据库

mysql - 如何通过 join 进行更新

mysql - 我正在尝试为我的 laravel 网站获取最近的聊天记录。但查询返回错误的输出

php - 使用选择选项更新多行