简体   繁体   English

MySQL IS NOT NULL和!=''之间的区别

[英]Difference between MySQL IS NOT NULL and != ''

Is there any difference between MySQL MySQL之间有什么区别吗?

IF (myText IS NOT NULL) THEN

and

IF (myText != '') THEN

Yes there is a big difference between a NULL value and a blank/empty value. 是的, NULL值和空白/空值之间存在很大差异。

Here's one resource that describes the differences. 这是描述差异的一种资源

When myText IS NULL : myText IS NULL

  • myText IS NOT NULL evaluates to FALSE myText IS NOT NULL计算结果为FALSE
  • myText != '' evaluates to NULL (which essentially behaves the same as FALSE would in this specific case you wrote) myText != ''求值为NULL (在你编写的这个特定情况下,它的行为与FALSE基本相同)

However, you should not get into the habit of treating them the same, since most of the time they will behave differently: For example: 但是,你不应该养成对待它们的习惯,因为大多数时候它们会表现不同:例如:

Assume you have a table tbl : 假设你有一个表tbl

id   text
1    NULL
2    
3    abc

Note: 1 contains a NULL value, and 2 contains an empty string ( '' ). 注意: 1包含NULL值,2包含空字符串( '' )。

If you run the following query: 如果您运行以下查询:

SELECT * FROM tbl WHERE text != ''

... it will return record 3. ......它将返回记录3。

If you run the following query: 如果您运行以下查询:

SELECT * FROM tbl WHERE text IS NOT NULL

... it will return records 2 and 3. ...它将返回记录2和3。

Yes there is a difference. 是,有一点不同。

In simple words, myText IS NOT NULL specifies that myText is having some value which could be '' too . 简单来说, myText IS NOT NULL指定myText具有某些可能也是“值”的值

Where as myText != '' specifies that it returns TRUE, if myText does NOT contain an empty string . 其中myText!=''指定它返回TRUE,如果myText不包含空字符串

There is a difference. 它们是有区别的。 If the default value of a column is "NULL", then if no data has been set for a field, it is truly null. 如果列的默认值为“NULL”,那么如果没有为字段设置数据,则它实际上为空。 However, if the value of a field has been updated as '', it is not NULL, rather it is empty. 但是,如果字段的值已更新为'',则它不是NULL,而是为空。

See here for more information Link 在这里看到更多的信息链接

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

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