2017-03-17 69 views
1

有沒有簡單的方法來重新創建此語句?或者我可以在我的數據庫中查找一個示例?因爲目前我不明白它到底做了什麼。我想我理解第一個in,但我不知道爲什麼有項目兩in條款爲什麼第二個例子是不一樣的第一個。提前致謝。瞭解嵌套的SQL語句

select usernumber as usrnr from DOCUMENTS 
where CompanyIndex in (
    select CompanyIndex 
    from DOCUMENTS 
    where DocumentType='3' 
     and ProjectsIndex in (select Index 
          from PROJECTS where 
          Projectnumber = 209806) 
) 
and ProjectsIndex in (
    select Index 
    from PROJECTS 
    where Projectnumber = 209806 
) 
and DocumentType = '2' 

而且我不明白爲什麼它是不一樣的,因爲這

select ad1 as AD1Nr from DOCUMENTS 
where CompanyIndex in 
(
    select CompanyIndex 
    from DOCUMENTS 
    where DocumentType='3' 
     and ProjectsIndex in (select Index 
          from PROJECTS 
          where Projectnumber = 209806) 
     and DocumentType = '2') 

回答

2

我重新格式化代碼來顯示的結構。

現在的區別很明顯:條件DocumentType = '2'位於第二個樣本的內部選擇內部,但在第一個樣本的外部。

(這就是爲什麼有些強迫性格式化的強迫性不是負面的特徵。)