Two minutes to get nested sql statement not exisits division wording

**

Example: Introduction Chapter Two after-school exercise Wang Shan version of the database system

**

Seeking at least with the supplier S1 supplied all the parts of the project number JNO


first to get this title, table involving only SPJ table, and see that use division to solve this problem easier. .

Can not exists division relates generally to multi-layer nested loops, bad master. I tell you about a short answer shortcut to write database division with sql statement. .

1. First results clearly need to query, dividend and divisor.

In this example, the query result is JNO question, SPJ table dividend is projected out PNO, JNP two, when the divisor SNO = 'S1' projected PNO.
We usually do this!
1.1 query results in the outermost
1.2 put on the intermediate layer divisor (plus divisor selection conditions)
1.3 put on the innermost layer and the dividend condition is introduced into the innermost layer

write sql statement to give

select Distinct JNO
From SPJ SPJX
where not exists
(	select * 
	from SPJ SPJY
	where SPJY.SNO='S1' AND not exists(
	select *
	from SPJ SPJZ
	where SPJZ.JNO = SPJX.JNO AND SPJZ.PNO = SPJY.PNO
	) 
)

Detailed analysis of the following steps:

-1 to clear check out the results of the dividend the divisor
- the question we need to check that JNO SPJ table dividend is projected out of the PNO, JNO two divisor is projected out when SNO PNO = 'S1' is
- we put the divisor after the first not exists and it plus the necessary conditions (conditions for its selection = SNO 'Sl')
- we put on after the second dividend not exists
- then the innermost layer and two outer query query where to find the associated attributes lead to statement

-2. The key step is to find out how the relationship between the association and the innermost layer of the outer layer of it?
- it can be seen that the connection between the outermost layer and the innermost rely JNO, the outermost layer and inner layer connection requires twice PNO.
Because dividend of two properties JNO, PNO. Divisor attributes SNO = 'S1' select PNO
dividend so the inner layer and the divisor to PNO attributes associated with the intermediate layer, the divisor JNO attributes associated with the outer JNO.
-3. Look how out of it?
- First Look divisor, the divisor is screened PNO attribute column. Directly in the inner layer = SPJZ.PNO write SPJY.PN0
- looking at the dividend, the dividend check out the result JNO. Written directly on the inner layer = SPJX.JNO SPJY.JNO
- then add the divisor of the selection criteria to complete the division sql statement

Guess you like

Origin blog.csdn.net/weixin_43752167/article/details/90676465