English 中文(简体)
筛选列表中的NULL
原标题:Filter for NULL in list

why does filter for NULL in subqueries does not work? I hoped to get the correct result by add NULL to the list of allowed values, for example:

SELECT     ERP_ServiceProcess.fiStatusNew, RMA.IdRMA
FROM         ERP_ServiceProcess RIGHT OUTER JOIN
                      RMA ON ERP_ServiceProcess.fiRMA = RMA.IdRMA
WHERE  (ERP_ServiceProcess.fiStatusNew IN (NULL, 1, 7, 8))
order by ERP_ServiceProcess.fiStatusNew

这给出了不正确的结果,因为删除了RMA中所有在子表ERP_ServiceProcess(其中ERP_ServiceProcess.fiStatusNew ISNULL)中没有记录的记录。

我必须使用此(慢速)查询才能获得正确的结果:

SELECT     ERP_ServiceProcess.fiStatusNew, RMA.IdRMA
FROM         ERP_ServiceProcess RIGHT OUTER JOIN
                      RMA ON ERP_ServiceProcess.fiRMA = RMA.IdRMA
WHERE     (ERP_ServiceProcess.fiStatusNew IS NULL)
OR (ERP_ServiceProcess.fiStatusNew IN (1, 7, 8))
order by ERP_ServiceProcess.fiStatusNew

尽管我使用了<code>RIGHT OUTER JOIN</code>并将<code>NULL</code〕添加到子查询中,但为什么我必须使用第二个较慢的查询?

提前谢谢。

最佳回答

它并没有像你预期的那样工作,因为它被扩展到了一堆相等的运算

fiStatusNew=空或fiStatusNew=1或fiStatus New=7或fiStatuesNew=8

并且anything=NULL是未知的。

考虑到这种扩展,没有特别的理由认为使用<code>IS NULL</code>添加一个额外的<code>OR</code<会使事情本身变得更慢(如果统计数据使其相信匹配行的数量保证了这一点,则额外的谓词可能会更改查询计划以使用不同的访问路径)

您可以在CASE操作中看到相同的行为

SELECT CASE NULL WHEN NULL THEN  Yes  ELSE  No  END /*Returns "No"*/

这就是为什么您应该特别注意反向操作NOT IN的原因之一。如果列表中包含任何<code>NULL</code>值,您将始终得到一个空的结果集。

fiStatusNew NOT IN(空,1,2)

将扩展到

fiStatusNew<>;NULL和fiStatusNew<>;1和fiStatusNew<>;2

未知和真/假/未知和真、假/未知

三值逻辑

问题回答

你能试着用吗

ISNULL(ERP_ServiceProcess.fiStatusNew,0) IN (0, 1, 7, 8)

未测试,但可能比第二个查询更快。

ERP_ServiceProcess.fiStatusNew IN(NULL)的计算结果为ERP_ServiceProcess.fiStatus New=NULL,并且始终为false。NULL在sql server中被定义为未知,而不是无值。这就是为什么NULL=NULL或NULL=@var(*)的计算结果总是为false的原因。如果有两个未知数,则无法检查它们是否相等。只有NULL有效。

(*)对于sql server,您可以设置ANSI_NULLS关闭,但这不是真正建议的,因为这不是标准的sql行为。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...