未选择 Oracle sql 空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14994100/
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-09-01 13:47:43  来源:igfitidea点击:

Oracle sql null value is not selected

sqloracle

提问by sunleo

In NAME table FIRST column is having null but no rows are selected. Please help me to understand.

在 NAME 表中,FIRST 列为空,但未选择任何行。请帮我理解。

SELECT * FROM NAME WHERE FIRST != '1'

回答by dasblinkenlight

Any comparison with nullis false - =and <>, >, <, and so on. You cannot use nullin an INlist as well - it would be ignored. Moreover, two nulls are noteven equal to each other.

任何与 的比较null都是假的 -=<>><等等。您也不能nullIN列表中使用- 它会被忽略。而且,两个nulls甚至相等。

To get the nulls, you need to ask for them explicitly, like this:

要获得空值,您需要明确要求它们,如下所示:

SELECT * FROM NAME WHERE FIRST IS NULL OR FIRST != '1'

回答by Gordon Linoff

Any comparison to NULL returns NULL, which is equivalent to FALSE. This is true eve of not-equals.

任何与 NULL 的比较都会返回 NULL,这相当于 FALSE。这是真正的不平等之夜。

If you want to include NULL values, do one of the following:

如果要包含 NULL 值,请执行以下操作之一:

where first <> '1' or first is null

or

或者

where coalesce(first, '<null>') <> '1'

回答by Joe

In Oracle, null is not considered a legal value to select unless you explicitly ask for it:

在 Oracle 中,除非您明确要求,否则 null 不被视为要选择的合法值:

select * from name where (first != '1') or first is null

You could also use NVL (similar to coalesce):

您也可以使用 NVL(类似于合并):

select * from name where nvl(first,'0') != '1'

回答by NirmalGeo

That is correct because NULL can never be compared with anything else....

这是正确的,因为 NULL 永远不能与其他任何东西进行比较......

The only option that you have is to include a NULL check as an or in the command

您唯一的选择是在命令中包含 NULL 检查作为或

SELECT * FROM NAME WHERE FIRST!=1 OR FIRST IS NULL

According to Oracle Documentation NULL is defined as a value not knownm or when the value is not meaningful. That is solely the reason why Oracle mentions not consider a value of ZERO as NULL. This is just an FYI, an addon. Thanks!

根据 Oracle 文档,NULL 被定义为未知值或值没有意义时。这就是 Oracle 提到不将 ZERO 值视为 NULL 的唯一原因。这只是一个仅供参考,一个插件。谢谢!

回答by AS400Jockey

NULL is dumb. Period.

NULL 是愚蠢的。时期。

NULL is evil.

NULL 是邪恶的。

If X is NULL and Y is NULL, then X does in fact equal Y because they are both NULL.

如果 X 为 NULL 且 Y 为 NULL,则 X 实际上等于 Y,因为它们都是 NULL。

It's also a PITA that I can't say

也是说不出来的PITA

IF X IN ('a','B','C', null)

IF X IN ('a','B','C', null)

Because this condition happens. But now I have to say

因为这种情况发生了。但现在我不得不说

IF ( X IN ('a','B','C') or X is NULL ) 

which is a waste of time and a risk of error if I forget the parentheses.

如果我忘记括号,这是浪费时间和出错的风险。

What irks me further is that NULL shouldn't happen in the first place. Fields (er... ok kids, I'll call them Columns) should always be initialized. Period. Stop the nulls. Don't allow them. Default values should always be zeroes or blanks so that those folks that are too lazy to initialize columns in their software will have them initialized for them automatically.

更让我烦恼的是 NULL 不应该首先发生。字段(呃……好吧,孩子们,我称它们为列)应该总是被初始化。时期。停止空值。不允许他们。默认值应该总是零或空白,这样那些懒得在他们的软件中初始化列的人会自动为它们初始化它们。

There are many instances where a failure to define default values of zeroes and blanks makes life more difficult than it has to be.

在许多情况下,未能定义零和空白的默认值会使生活变得更加困难。