Executes each row of data and returns its records. Then, the external query makes a decision based on the returned records.
The following uses the northwind database as an example. We need to list the latest releases from each customer.
.
Select o1.mermerid, o1.orderid, o1.orderdate
From orders O1
Where o1.orderdate =
(Select max (orderdate)
From orders O2
Where o2.customerid = o1.customerid)
For each row of data returned by an external query, the internal query is executed on the condition that the result set and
Customerid match. Max () calls limit the result set to a row of data of interest.
If 500 customers place orders, internal queries are performed 500 times-each customerid is executed once.
Now we have an answer: Return 90 rows of data, that is, each of the 90 rows of data represents one
The customer issued an order.
You can easily scale this query. For example, you may want to query the latest orders placed by each customer.
Two orders. In this case, you need to change the internal query to the following form:
Select o1.mermerid, o1.orderid, o1.orderdate
From orders O1
Where o1.orderdate in (
Select Top 2 o2.orderdate
From orders O2
Where o2.customerid = o1.customerid)
Order by customerid
You can optimize this example in different ways to avoid overhead in () predicates.
In my experience, many developers will soon forget the concept of correlated subqueries.
This is really a pity, because correlated subqueries can easily and elegantly answer difficult questions.
Question.
In my opinion, this syntax is feasible and an SQL statement can be used to query the expected results. This makes me think
A Development Manager repeatedly said to me: "The first version is to make it run, and the second version
Is to make it run faster ."
Nested subquery example:
Select nbbm, (select ypm + Gg from zy_sys2_ypzdk where
Zy_sys2_ypzdk.nbbm = zy_yp1_sfmx.nbbm) as PM, bzsl from
Zy_yp1_sfmx
Query the customer's latest five orders (extensions), which is also very classic
Select o1.mermerid, o1.orderid, o1.orderdate
From orders O1
Where o1.orderdate in (
Select top 5 o2.orderdate
From orders O2
Where o2.customerid = o1.customerid)
Order by customerid
Http://lovewinter.itpub.net/post/493/6435