2011-11-04 79 views
0

也許我一直在尋找這個太長時間 - 但有人可以幫我看看爲什麼這兩個查詢,這應該返回相同的東西,返回不同數量的行?爲什麼這兩個「等價」查詢返回不同數量的行?

select ip.* 
from invoice_payment ip 
inner join invoice_item II on ii.invoice_item_uuid = ip.invoice_item_uuid 
inner join service_delivery sd on sd.service_delivery_uuid = ii.service_delivery_uuid 
inner join #Affected on #Affected.service_delivery_uuid = sd.service_delivery_uuid 


select * 
from invoice_payment ip 
where ip.invoice_item_uuid in (
    select ii.invoice_item_uuid from invoice_item ii 
    where ii.service_delivery_uuid in (
    select service_delivery_uuid from #Affected) 
) 

謝謝!

+0

他們不是等效查詢 – sll

回答

4

第一個返回一個產品:

IP x II x sd x #Affected 

然後將產物由on條件的限制。但是,如果任何條件匹配多行,則最終的行數將超過IP表中的行數。

第二個查詢返回IP中符合特定條件的行。第二個查詢永遠不會返回比IP中更多的行。

+0

謝謝Andomar。那麼你是說第二個更準確嗎?我將把這個選擇變成刪除,所以重要的是我要得到正確的結果。 – user477526

+0

是的,第二個會更準確地捕獲刪除。 – Andomar

1

如果任何表invoice_item,service_delivery或#Affected包含與其JOIN所對應的表對應的多個記錄,那麼這將增加結果集中的記錄數,從而有效地乘以報告了invoice_payment。

因爲在我看來,invoice_payment可能對應多個invoice_item,這就是我首先看的地方。

順便提一下,這兩個查詢甚至不是大致相等的。

相關問題