Two solutions to "Find the project number of at least all parts supplied by supplier "S1""
1. Question design
Database SPJ contains 4 tables:
Supplier table S (code Sno, name Sname, status, city)
Sno | It takes off | Status | City |
---|
Parts list P (code Pno, name Pname, color Color, weight Weight)
Pno | Pname | Color | Weight |
---|
Project project table J (code Jno, name Jname, city)
Jno | Jname | City |
---|
Supply status table Spj (supplier code Sno, part code Pno, item code Jno, quantity Qty)
Sno | Pno | Jno | Qty |
---|
The problem is: at least the engineering number JNO of all parts supplied by engineering S1 is used
2. Method 1: Direct solution
Idea: Solve directly, first find the set of all parts P1 supplied by the S1 supplier, and then find the set of parts Pn used by each project. If P1⊆Pn, it means that the project uses all the parts supplied by the S1 supplier. .
(select b.Pno from Spj as b where b.Sno='S1') as d //S1供应商供应的全部零件集合P1
(select c.Pno from Spj as c where c.Jno='某一工程代号') //某一工程使用的零件集合
To determine whether P1⊆Pn is true or false, only the difference set between P1 and Pn is required, that is, P1-Pn. If P1-Pn is the empty set (the query result does not exist), it means P1⊆Pn. Note: It can be used to find the difference set of AB
in Mysql .select * from A where * not in B;
select * from
(select b.Pno from Spj as b where b.Sno='S1') as d
where d.Pno not in
(select c.Pno from Spj as c where c.Jno='某一工程代号')
//若查询返回结果为空,则说明该工程没有使用所有的S1提供的零件。
Then use it not exists
to determine whether a certain project meets the conditions, and then use the a.Jno
replacement '某一工程代号'
placeholder to traverse each project in the J table.
select Jno from J as a where not exists(
select * from
(select b.Pno from Spj as b where b.Sno='S1') as d
where d.Pno not in
(select c.Pno from Spj as c where c.Jno=a.Jno)
);
3. Method 2: Indirect solution
Many blogs have written about it, so I won’t go into details.