2017-03-17 82 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'位于第二个样本的内部选择内部,但在第一个样本的外部。

(这就是为什么有些强迫性格式化的强迫性不是负面的特征。)