嵌套的isNull查询中的where子句给出了意外的结果

时间:2017-07-20 11:13:22

标签: sql sql-server where-clause isnull

以下查询返回大约200000个结果。

在这个查询中嵌套where子句的工作不是很清楚,即它出现在图片中的哪个位置?

如果我在isNull中注释掉where子句,那么我得到0结果,这很好并且预期因为加入后Max(invoiceID)不为null。

select * from CustomerServices where isNull((
                                SELECT MAX(invoiceid) 
                                FROM Invoices 
                                    LEFT JOIN InvoicesHistory
                                        ON InvoicesHistory.ServiceHistoryID = Invoices.ServiceHistoryID 
                                WHERE serviceID = Invoices.serviceID
                            ),0)=0

如果您希望我添加更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

我想你只想要get

not exists

在您的情况下,嵌套的select cs.* from CustomerServices cs where not exists (select 1 from Invoices i left join InvoicesHistory ih on ih.ServiceHistoryID = i.ServiceHistoryID where cs.serviceID = i.serviceID ); 子句没有做任何事情。它相当于:

WHERE

通过SQL中的作用域规则。很可能,这是一个相关子句,因此需要一个合格的列名。

相关问题