SQL/MySQL NOT NULL 与 NOT EMPTY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16393806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:32:38  来源:igfitidea点击:

SQL/MySQL NOT NULL vs NOT EMPTY

mysqlsqlnull

提问by cream

I'd like to limit my query to show only rows where a certain field is not empty. I found this threadwhere someone posed the same question and was told to use IS NOT NULL. I tried that, but I'm still getting rows where the field is empty.

我想将我的查询限制为仅显示某个字段不为空的行。我发现这个线程如果有人提出同样的问题,被告知使用IS NOT NULL。我试过了,但我仍然得到字段为空的行。

What is the correct way to do this? Is Null the same thing as Empty in SQL/MySQL?

这样做的正确方法是什么?Null 与 SQL/MySQL 中的 Empty 是否相同?

My query, if you're interested is: SELECT * FROM records WHERE (party_zip='49080' OR party_zip='49078' OR party_zip='49284' ) AND partyfn IS NOT NULL

我的查询,如果你有兴趣是: SELECT * FROM records WHERE (party_zip='49080' OR party_zip='49078' OR party_zip='49284' ) AND partyfn IS NOT NULL

采纳答案by Robin Castlin

When comparing a NULLvalue, the result in most cases becomes NULLand therefor haves the same result as 0(the FALSE value in MySQL) inside WHEREand HAVING.

当比较一个NULL值,结果在大多数情况下变得NULL和为此富人相同的结果0的内部(在MySQL的FALSE值)WHEREHAVING

In your given example, you don't need to include IS NOT NULL. Instead simply use party_zip IN ('49080', '49078', '49284'). NULLcan't be 49080, 49078, 49284 or any other number or string.

在您给定的示例中,您不需要包含IS NOT NULL. 而是简单地使用party_zip IN ('49080', '49078', '49284'). NULL不能是 49080、49078、49284 或任何其他数字或字符串。

What you do need to think about though, is when checking for empty values. !party_zipwon't return TRUE/1if the value is NULL. Instead use OR columns IS NULLor !COALESCE(party_zip, 0)

但是,您需要考虑的是检查空值时。如果值为 ,则!party_zip不会返回TRUE/ 。而是使用或1NULLOR columns IS NULL!COALESCE(party_zip, 0)

回答by cream

I got it by using AND (partyfn IS NOT NULL AND partyfn != '')

我通过使用得到它 AND (partyfn IS NOT NULL AND partyfn != '')