web123456

Database related subquery and non-associated subquery

Database associationSubqueryand non-associated subquery


Non-associated subquery: Inner queries in database nested queries are completely independent of outer queries.
Execution order:

  • Execute inner-level query first
  • After obtaining the results of the inner layer query, bring them into the outer layer, and then performing the outer layer query.
select * from tableA where tableA.column  = (select tableB.column from tableB )
  • 1

Associated subquery: Inner queries and outer queries are not independent of each other in database nested queries, and inner queries also rely on outer queries.
Execution order:

  • First query a record from the outer query
  • Then put the query records into the records that meet the conditions in the inner layer query, and then put them into the outer layer for query.
  • Repeat the above steps
    For example:
select * from tableA where tableA.cloumn < (select column from tableB where tableA.id = tableB.id))
  • 1
  • So: first query a record of tableA;
  • Enter the inner layer query, and according to the found record that satisfies the same as it is found in tableB, if the column value that matches the record in table A < the column value of the record in table B, then the record in table A meets the conditions, the query outputs a record.
  • Find the next record of the outer query and repeat the above steps until the table A record query is completed.
    From this we can see that the query of the associated subquery is carried out crosswise.