OK, a little hard to explain. (And sorry for the lengthy question)
tables :
I have Product1, Product2, Product3 in tblProducts.
I also have 10 processes in tblProcesses.
And this is what I have in the junction table between them :
Objective :
I have the solution for part 1:
My question:
A test db is attached.
tables :
I have Product1, Product2, Product3 in tblProducts.
I also have 10 processes in tblProcesses.
And this is what I have in the junction table between them :
Code:
---------------------------------------------
| ProductsProcessPK | ProductFK | ProcessFK|
|-------------------|------------|----------|
| 1 | 1 | 1 |
|-------------------|------------|----------|
| 2 | 1 | 3 |
|-------------------|------------|----------|
| 3 | 1 | 5 |
|-------------------|------------|----------|
| 4 | 2 | 3 |
|-------------------|------------|----------|
| 5 | 2 | 5 |
|-------------------|------------|----------|
| 6 | 2 | 7 |
|-------------------|------------|----------|
| 7 | 3 | 3 |
|-------------------|------------|----------|
| 8 | 3 | 8 |
---------------------------------------------
Objective :
- Add all ProcessFKs of Product1 and Product2 in the junction table to Product3 that already doesn't exist. ( Add 1, 5 , 7)
Why?
Processes of ProductFK1 --->1 - 3 - 5
Processes of ProductFK2 ---> 3 - 5 - 7
Processes of ProductFK3 ---> 3 - 8 ---> (1 - 5 - 7 is not included. Add it)
- Delete any ProcessFK for Product3 that is not present for Product1 And product2.
(Delete 8 Because neither ProductFK1 Nor ProductFK2 has processFK 8)
I have the solution for part 1:
SQL:
INSERT INTO
tblProductsProcesses ( ProductFK, ProcessFK )
SELECT
3 AS AddThis,
PP.ProcessFK
FROM
tblProductsProcesses AS PP
WHERE
PP.ProductFK In (SELECT S.ProductPK FROM tblProducts AS S WHERE S.ProductPK IN (1,2))
GROUP BY
PP.ProcessFK
HAVING
PP.ProcessFK Not In (SELECT ProcessFK FROM tblProductsProcesses WHERE ProductFK=3));
My question:
- Since all tables are very very large, is there any correction needed for first query? (According to my tests it's OK, but people like you may find something unnecessary)
- I couldn't come up with the delete query. Can you help with a sql that deletes unnecessary (and almost saved) records for Product3?
A test db is attached.