简体   繁体   English

MySQL> 添加整数和 null 值

[英]MySQL> Adding integers and null values

Question : How can we correctly handle an arithmetic operation across different columns of a table where at least one of the operands is NULL?问题:我们如何正确处理至少一个操作数是 NULL 的表的不同列的算术运算?


Background: We have some data that was mined from the public domain and imported into MySQL.背景:我们有一些从公共领域挖掘出来的数据,并导入到 MySQL。 There are some text fields and some integer fields.有一些文本字段和一些 integer 字段。 The data miner treated all missing fields as NULL, rather than assign a value of 0 to the missing integer fields and NULL to the missing text fields.数据挖掘器将所有缺失的字段视为 NULL,而不是将值 0 分配给缺失的 integer 字段,并将 NULL 分配给缺失的文本字段。

I am able to recreate our issue with this simple table definition:我可以用这个简单的表定义重新创建我们的问题:

CREATE TABLE employee
(
    id     INT AUTO_INCREMENT,
    name   VARCHAR(40),
    salary INT,
    bonus  INT,
    
    PRIMARY KEY(id)
);  

I am populating this sample table as follows我正在按如下方式填充此示例表

INSERT INTO employee(name, salary, bonus)
VALUES('Keith', 120000, 10000), ('Larson', 110000, 8000),
('Angelina', 125000, 12500), ('Will', 75000, null),
('Mary', 80000, null), ('Steve', null, null), ('John', null, null);

The query查询

SELECT * FROM employee;

yields产量

+----+----------+--------+-------+
| id | name     | salary | bonus |
+----+----------+--------+-------+
|  1 | Keith    | 120000 | 10000 |
|  2 | Larson   | 110000 |  8000 |
|  3 | Angelina | 125000 | 12500 |
|  4 | Will     |  75000 |  NULL |
|  5 | Mary     |  80000 |  NULL |
|  6 | Steve    |   NULL |  NULL |
|  7 | John     |   NULL |  NULL |
+----+----------+--------+-------+

For arithmetic operations, the NULL values within a column are treated as 0s.对于算术运算,列中的 NULL 值被视为 0。 That is why这就是为什么

SELECT SUM(salary) FROM employee;

yields产量

+-------------+
| SUM(salary) |
+-------------+
|      510000 |
+-------------+

However, when an arithmetic operation is performed across columns, and at least one of the operands is NULL, the value returned is NULL.但是,当跨列执行算术运算,并且至少有一个操作数是 NULL 时,返回的值是 NULL。 For example:例如:

SELECT name, (salary + bonus) FROM employee;

yields产量

+----------+------------------+
| name     | (salary + bonus) |
+----------+------------------+
| Keith    |           130000 |
| Larson   |           118000 |
| Angelina |           137500 |
| Will     |             NULL |
| Mary     |             NULL |
| Steve    |             NULL |
| John     |             NULL |
+----------+------------------+  

I would like my query to return 75000 for Will and 80000 for Mary (in addition to the correct values for Keith, Larson and Angelina).我希望我的查询为 Will 返回 75000,为 Mary 返回 80000(除了 Keith、Larson 和 Angelina 的正确值之外)。

use coalesce .使用coalesce Here is the demo .这是演示

SELECT 
    name, 
    (salary + coalesce(bonus, 0)) as total
FROM employee;

output: output:

| name     | total  |
| -------- | ------ |
| Keith    | 130000 |
| Larson   | 118000 |
| Angelina | 137500 |
| Will     | 75000  |
| Mary     | 80000  |
| Steve    | null   |
| John     | null   |

On the safe side you can use (coalesce(salary, 0) + coalesce(bonus, 0)) , so your result won't show null .为了安全起见,您可以使用(coalesce(salary, 0) + coalesce(bonus, 0)) ,因此您的结果不会显示null

You can use IFNULL.您可以使用 IFNULL。

SELECT 
name, 
salary + IFNULL(bonus, 0) as total
FROM employee;

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

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