sql如何处理null值_如何正确处理SQL中的NULL值

sql如何处理null值

前言 (Preface)

A friend who has recently started learning SQL asked me about NULL values and how to deal with them. If you are new to SQL, this guide should give you insights into a topic that can be confusing to beginners.

最近开始学习SQL的一个朋友问我有关NULL值以及如何处理它们的信息。 如果您不熟悉SQL,则本指南应为您提供一个可能使初学者感到困惑的主题的见解。

什么是NULL? (What is NULL?)

NULL is used in SQL to indicate that a value doesn’t exist in the database.It’s not to be confused with an empty string or a zero value.

SQL中使用NULL表示数据库中不存在任何值,请勿将其与空字符串或零值混淆。

While NULL indicates the absence of a value, the empty string and zero both represent actual values. To use an analogy, just as the lack of an answer doesn’t necessarily mean the answer is “no”, the lack of a value doesn’t mean it is zero.

NULL表示缺少值,而空字符串和零都表示实际值。 用类推,就像缺少答案并不一定意味着答案是“否”一样,缺少值并不意味着它就是零。

Misunderstanding how NULL works in SQL can result in unexpected errors and output, so without further ado, let’s see how we can tackle it.

误解NULL在SQL中的工作方式会导致意外的错误和输出,因此,不费吹灰之力,让我们看看如何解决它。

For this exercise, we will work with the tblSouthPark table.

对于本练习,我们将使用tblSouthPark表。

SELECT *FROM tblSouthPark

1. IS NULL和IS NOT NULL运算符 (1. IS NULL and IS NOT NULL Operators)

We cannot use the comparison operators, =,<,>,<>, to test for NULL values. Instead, we have to use IS NULL and IS NOT NULL predicates.

我们不能使用比较运算符=,<,>,<>来测试NULL值。 相反,我们必须使用IS NULL和IS NOT NULL 谓词

  • IS NULL: Return rows that contain NULL values

    IS NULL 返回包含NULL值的行

Syntax: expression IS NULL

语法:表达式IS NULL

SELECT 
ID,
Student,
Email1,
Email2FROM tblSouthParkWHERE Email1 IS NULL AND Email2 IS NULL
ORDER BY
ID

The above query yields all records where both Email1 and Email2 columns are NULL.

上面的查询将产生所有记录,其中Email1和Email2列均为NULL。

  • IS NOT NULL: As the name suggests, it is the inverse of IS NULL.

    IS NOT NULL:顾名思义,它与IS NULL相反。

Syntax: expression IS NOT NULL

语法:表达式不为空

SELECT 
ID,
Student,
Email1,
Email2FROM tblSouthPark
WHERE
Email1 IS NOT NULL AND Email2 IS NOT NULL
ORDER BY
ID

The above query yields all records where both Email1 and Email2 columns are NOT NULL.

上面的查询将产生所有记录,其中Email1和Email2列都不为空。

2. ISNULL()函数 (2. ISNULL() Function)

The ISNULL function returns the specified value if the given expression is NULL. Otherwise, if the expression is not NULL, it returns the expression itself.

如果给定表达式为NULL,则ISNULL函数将返回指定值。 否则,如果表达式不为NULL,它将返回表达式本身。

Syntax: ISNULL(expression, value)

语法: ISNULL(表达式,值)

Let’s understand this by running a simple query on our SouthPark table.

让我们通过在SouthPark表上运行一个简单的查询来了解这一点。

SELECT 
ID,
Student,
ISNULL(Father,'Missing') AS FatherFROM tblSouthParkORDER BY ID

This query yields the following result:

该查询产生以下结果:

Since the expression in the case of Eric Cartman evaluated to NULL, the ISNULL function returned the value, Missing. For other students, the expression was not NULL so it returned the expression itself.

由于在Eric Cartman的情况下表达式的计算结果为NULL,因此ISNULL函数返回值Missing 。 对于其他学生,该表达式不是NULL,因此它返回了表达式本身。

3. COALESCE()函数 (3. COALESCE() Function)

The COALESCEfunction returns the first non-NULL value in a given list. Unlike the ISNULL function, it can accept multiple expressions.

COALESCE函数返回给定列表中的第一个非NULL值。 与ISNULL函数不同,它可以接受多个表达式。

Syntax: COALESCE(expression[1…..n])

语法: COALESCE(表达式[1…..n])

For example, SELECT COALESCE(NULL, NULL, 'red', 'blue', NULL) returns red as it’s the first non-NULL value. If all the values are NULL, the COALESCE function will return NULL.

例如, SELECT COALESCE (NULL, NULL, 'red', 'blue', NULL)返回red因为它是第一个非NULL值。 如果所有值均为NULL,则COALESCE函数将返回NULL。

Let us use the COALESCE function on our SouthPark table:

让我们在SouthPark表上使用COALESCE函数:

SELECT 
ID,
Student,
COALESCE(Email1, Email2, 'N/A') AS Primary_EmailFROM tblSouthParkORDER BY ID

The above query yields the following result:

上面的查询产生以下结果:

As expected, since both Email1 and Email2 are null in Kenny’s case, the COALESCE function returns N/A as the Primary_Email. For Stan, Email2 is returned as the Primary_Email as it is the first non-NULL value in the COALESCE function. For others, Email1 is returned as the Primary_Email.

不出所料,由于在Kenny的情况下Email1和Email2均为null,因此COALESCE函数将N/A作为Primary_Email返回。 对于Stan,Email2作为Primary_Email返回,因为它是COALESCE函数中的第一个非NULL值。 对于其他邮件,Email1作为Primary_Email返回。

4.案例表达 (4. CASE Expression)

We can also use the good old CASE expression to replace NULL values.

我们还可以使用良好的旧CASE表达式替换NULL值。

Here is the general syntax for the CASE expression:

这是CASE表达式的一般语法:

CASE
WHEN expression_1 THEN result_1
WHEN expression_2 THEN result_2
.
.
.
WHEN expression_n THEN result_n
ELSE else_expressionEND

We can generate the same output we got using COALESCE above by using CASE.

我们可以使用CASE生成与上面使用COALESCE得到的输出相同的输出。

The query will look like this:

查询将如下所示:

SELECT 
ID,
Student,
CASE

WHEN Email1 IS NOT NULL THEN Email1
WHEN Email2 IS NOT NULL THEN Email2
ELSE 'N/A'
END AS Primary_EmailFROM tblSouthParkORDER BY ID

This query yields the same output:

此查询产生相同的输出:

Note: The CASE expression is syntactically similar to the COALESCE function. In fact, COALESCE is like a shorthand for CASE. The former is short and simple, but the latter is more clear and easy to understand.

注意: CASE 表达式在语法上类似于 COALESCE 函数。 实际上, COALESCE 就像 CASE 的简写 前者简短明了,但后者更清晰易懂。

5. NULLIF()函数 (5. NULLIF() Function)

The NULLIF function takes two expressions and returns NULL if the expressions are equal, or the first expression otherwise.

NULLIF函数采用两个表达式,如果两个表达式相等,则返回NULL,否则返回第一个表达式。

Syntax: NULLIF(expression_1, expression_2)

语法: NULLIF(表达式_1,表达式_2)

NULLIF('Red','Orange') -- Returns RedNULLIF(0,NULL) -- Returns 0NULLIF(0,0) -- Returns NULL

Where NULLIF comes in handy is in case of data that contains a mixture of null and empty strings in a column. Let’s understand this with an example.

如果数据在列中包含空字符串和空字符串的混合物, NULLIF会很方便。 让我们通过一个例子来理解这一点。

We see that the Phone column in our table contains both NULL and empty strings.

我们看到表中的Phone列同时包含NULL和空字符串。

We can standardize this by changing the empty string to NULL using NULLIF:

我们可以通过使用NULLIF将空字符串更改为NULL来对此进行标准化:

SELECT 
ID,
Student,
NULLIF(Phone,'') AS PhoneFROM tblSouthParkORDER BY ID

The above query yields:

上面的查询产生:

Another good use case for NULLIF is to prevent “division by zero” errors:

NULLIF另一个好用例是防止“被零除”错误:

var1 = 1
var2 = 0var1/var2 --This will generate a "division by zero" errorvar1/NULLIF(var2,0)--This doesn't trigger a "division by zero" error

In the second case, we do not get a “division by zero” error as NULL is returned in the denominator in place of 0.

在第二种情况下,由于分母返回NULL而不是0,所以我们没有得到“被零除”的错误。

奖金提示 (Bonus Tips)

  • TheCOALESCE function is standard ANSI, while ISNULL and NULLIF are not. This makes COALESCE more general and portable (across different SQL flavors and RDBMS) than ISNULL and NULLIF.

    COALESCE函数是标准ANSI,而ISNULLNULLIF不是。 与ISNULLNULLIF相比,这使COALESCE更具通用性和可移植性(适用于不同SQL风格和RDBMS)。

  • NULL is the smallest value in the sorting order. If we order by a column that contains NULL values, then the rows with NULL values will sort at the top by default. Use the DESC sort order to sort in reverse.

    NULL是排序顺序中的最小值。 如果我们按包含NULL值的列排序,则默认情况下,具有NULL值的行将排在最前面。 使用DESC排序顺序进行反向排序。

Tip: To sort in alphabetical order (ASC) with NULL values at the end, you can use a CASE expression in the ORDER BY clause:ORDER BY
CASE
WHEN column_name IS NULL THEN 1 ELSE 0 END,column_name;
  • The aggregate functions in SQL (SUM, COUNT, AVG, MAX, MIN) do not handle NULL values and eliminate them before performing any calculations. The only exception to this is the COUNT(*) function — it returns the count of all rows, including those rows where all fields are NULL.

    SQL中的聚合函数( SUMCOUNTAVGMAXMIN )不处理NULL值,并在执行任何计算之前将其消除。 唯一的例外是COUNT(*)函数-它返回所有行的计数,包括所有字段均为NULL的行。

  • In the case of GROUP BY clause, if a column contains rows with NULL values, then those will be grouped into one group.

    对于GROUP BY子句,如果一列包含具有NULL值的行,则这些行将被分组为一组。

  • You can chain COALESCE and NULLIF functions in cases where a column contains both NULL and empty strings and you need to create a consolidated column. For example:

    如果列同时包含NULL和空字符串,并且您需要创建一个合并的列, NULLIF可以链接COALESCENULLIF函数。 例如:

INPUT TABLE: Sample
+==============+==============+
| Work | Cell |
+==============+==============+
| | 717-735-6382 |
+--------------+--------------+
| 546-373-9363 | 493-353-3638 |
+--------------+--------------+
| NULL | 657-428-3639 |
+--------------+--------------+QUERYSELECT COALESCE(NULLIF(Work,''),Cell) AS Primary FROM SampleOUTPUT
+==============+
| Primary |
+==============+
| 717-735-6382 |
+--------------+
| 546-373-9363 |
+--------------+
| 657-428-3639 |
+--------------+We can also use a quick-and-dirty CASE expression to do the same:CASE WHEN Work <> '' THEN Work ELSE Cell END

结语 (Wrapping Up)

You should now have all you need to successfully tackle NULL values in SQL. Of course, we did not cover each and every edge case here, but this should be a good starting point for a beginner. If you get stuck, Stack Overflow is your friend. As always, practice is key!

现在,您应该拥有在SQL中成功处理NULL值所需的全部内容。 当然,我们没有在这里介绍每一个边缘情况,但这对于初学者来说应该是一个很好的起点。 如果您遇到困难, Stack Overflow是您的朋友。 与往常一样,练习是关键!

翻译自: https://medium.com/better-programming/how-to-deal-with-null-values-in-sql-the-right-way-69861f2debbf

sql如何处理null值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值