問題描述
看我的例子,兩個代碼有什么區別?
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 和 <>' 有什么區別'的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!