I have two tables:
TESTS:
ID | YEAR | DESCRIPTION |
---|---|---|
0 | 2023 | some text |
.. | … | … |
N | 2024 | some text |
test_prep_materials:
ID | PATH | TID |
---|---|---|
0 | /home/user/file0.pdf | 0 |
.. | … | … |
M | /home/user/fileM.pdf | 0 |
So any test have prep materials. And some test’s have same material.
I want to delete all material’s for 2023 tests and DELETE FILES FROM DISK. But 2024 test’s may have same path (SAME FILE ON DISK).
So i need to pick all prep materials for 2023 tests and exclude from them all path’s which have dependencies in 2024 tests.
I have tried the following, but there are too many results to know if the command was executed correctly.
SELECT `test_prep_materials`.`PATH`, `tests`.`id`, `tests`.`year`, `tests`.`Name_t`
from `test_prep_materials`
JOIN `tests` ON `test_prep_materials`.`tId` = `tests`.`id`
WHERE `tests`.`year` = 2023
EXCEPT
SELECT `test_prep_materials`.`PATH`, `tests`.`id`, `tests`.`year`, `tests`.`Name_t`
from `test_prep_materials`
JOIN `tests` ON `test_prep_materials`.`tId` = `tests`.`id`
WHERE `tests`.`year` <> 2023
Can somebody help me understand if my Query right or i am missing something?
2
Answers
It looks like you need paths to delete from past years tests that are not used in any test of this year. If that is the case that means you need paths used in last year and last year only – use Max(YEAR) = Min(YEAR) per PATH and < Max(YEAR) over all:
OR with all the data from tests table:
NOTE:
If you want to reffer to any specific year within a range (like 2021 till 2026) change the subquery selecting Max(YEAR) over all with the specific year – lets say 2025 – and the result should be paths used before 2025 and not in 2025. That was not in your sample data, though.
This can be tuned even more with different Where clause conditions.
You may use following query, it’ll eliminate the common path across both the years & gives you the path that you can delete.