简体   繁体   中英

Understanding nested SQL statement

Is there an easy way to recreate this statement? Or a way I can look it up for an example in my database? Because at the moment I don't understand what it exactly does. I think I understand the first in but I don't know why there are for PROJECTS two in clauses why the second example is not the same as the first. Thanks in advance.

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'

And I don't understand why it is not the same as this

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')

I've reformatted the code to show the structure.

The difference is now obvious: condition DocumentType = '2' is inside the inner select in the second sample but outside in the first.

(This is why being somewhat obsessive compulsive about formatting isn't a negative trait.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM