SQL是否在不等于查询中不显示空值? [英] SQL not displaying null values on a not equals query?

查看:251
本文介绍了SQL是否在不等于查询中不显示空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是出于好奇而提出的一个问题,但是我正在查看一个数据库,并通过对其中一列的查询从表中提取数据.该列具有四个可能的值null012.当我以以下方式运行查询时:

This is just a question out of curiosity but I am looking at a database and pulling data from a table with a query on one of the columns. The column has four possible values null, 0, 1, 2. When I run the query as:

SELECT * FROM STATUS WHERE STATE != '1' AND STATE != '2';

我得到与跑步相同的结果:

I get the same results as running:

SELECT * FROM STATUS WHERE STATE = '0';

即在查询列的top命令中,顶部命令中具有空值的行似乎被忽略了,这是否总是在SQL中发生?

I.e. rows with a null value in the top command in the queried column seem to be omitted from the results, does this always happen in SQL?

我正在通过Oracle SQL Developer运行命令.

I'm running my commands through Oracle SQL Developer.

推荐答案

在几种语言中,NULL的处理方式不同:大多数人都知道二值逻辑,其中truefalse是布尔表达式中唯一可比较的值(即使是false,也定义为0,其他则为true.

In several languages NULL is handled differently: Most people know about two-valued logic where true and false are the only comparable values in boolean expressions (even is false is defined as 0 and true as anything else).

在标准SQL中,您必须考虑三值逻辑. NULL不被视为真实值,您可以将其称为未知".因此,如果该值未知,则不清楚在您的情况下state是0、1还是其他值.因此NULL != 1的结果再次变为NULL.

In Standard SQL you have to think about three-valued logic. NULL is not treated as a real value, you could rather call it "unknown". So if the value is unknown it is not clear if in your case state is 0, 1, or anything else. So NULL != 1 results to NULL again.

这得出结论,无论您何时过滤可能为NULL的内容,都必须自己处理NULL值.请注意,语法也有所不同:NULL值只能与x IS NULL进行比较,而不能与x = NULL进行比较.有关显示逻辑运算结果的真值表,请参见Wikipedia.

This concludes that whereever you filter something that may be NULL, you have to treat NULL values by yourself. Note that the syntax is different as well: NULL values can only be compare with x IS NULL instead of x = NULL. See Wikipedia for a truth table showing the results of logic operations.

这篇关于SQL是否在不等于查询中不显示空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆