2012-05-03 36 views
0

根據sql server,Null不等同於sql中的任何東西,但以下查詢將返回已放置順序的所有產品。如何在嵌套查詢中使用null的子句?

Select * from products 
where exists (select null from orderdetails 
where orderdetails.product_id = products.product_id 
+0

Null = Null返回false –

+0

行爲可切換到SQLServer中,http://msdn.microsoft.com/en-us/library/aa259229(SQL.80).aspx。在ANSI SQL中,NULL = NULL返回false,如同NULL <> NULL。 –

回答

3

Exists測試是否存在行。它不檢查值。您可以使用where exists (select * ...)where exists(select 1 ...)。它不會有所作爲。

0

嘗試,代替select null,選擇ID,主鍵。

1

exists子句檢查是否存在:子查詢是否返回任何數據。它不擔心數據本身。

exists如果子查詢返回任何東西爲真。

1

存在如果子查詢包含任何行,則返回TRUE。

你在做什麼是Select Null;這將返回一行爲null,因此條件將爲真

2

Exists測試以查看包含的語句是否返回任何行。

我們將按照這一步一步一步來做。


select null from orderdetails 
where orderdetails.product_id = products.product_id 

返回包含null在ORDERDETAILS表與給定PRODUCT_ID每個訂單中的行。如果子查詢返回任何行


exists (select null from orderdetails 
where orderdetails.product_id = products.product_id) 

返回真(如果有訂單與PRODUCT_ID表,我們將有一個包含null行)


Select * from products 
where exists (select null from orderdetails 
where orderdetails.product_id = products.product_id) 

返回的每一件產品訂單表中存在任何訂單。