问题描述
看我的例子,两个代码有什么区别?
Look my example, what's the difference between two codes?
Select name from customers where name is not null
Select name from customers where name <> ''
推荐答案
他们做完全不同的事情.
They do completely different things.
Select name from customers where name is not null
此选项选择在名称字段中具有值的任何客户.这些值可以包括"以及诸如山姆"、约翰琼斯"、漂亮的金发女孩"之类的内容.
This one selects any customer who has a value in the name field. Those values can include '' as well as things like 'Sam', 'John Jones', 'pretty blonde girl'.
Select name from customers where name <> ''
这将选择至少在 Sql Server 中不为空或空白的所有名称.其他数据库可能会以不同的方式处理此问题.它还排除 Null 的原因是 Null 不能作为比较的一部分,因为它的定义意味着我们不知道该字段的值是什么.
This will select all names that are not null or blank In Sql Server at least. Other databases may handle this differently. The reason why it also excludes Null is that Null cannot be part of a comparison since it by definition means we don't have a clue what the value of this field is.
如果您想同时返回真实姓名和空值,并且只排除空字符串.在 SQL Server 中,您将执行以下操作:
If you wanted to return both real names and null values and only exclude the empty strings. In SQl Server you would do:
Select name from customers where coalesce(name, 'Unknown') <>''
这篇关于is not null 和 <>' 有什么区别'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!