I have table files
Also, I have more that 100 tables where I use relation/foreign key as file_id
How can I get all files
with non-existing relations (it means that file not attached to any table)?
I have table files
Also, I have more that 100 tables where I use relation/foreign key as file_id
How can I get all files
with non-existing relations (it means that file not attached to any table)?
2
Answers
As I understand your question, you are looking for all entries in table
files
where there is no entry in any of the other tables (xyz1, xyz2, xyz3) referencing that said file. In the following example, your want files 2 and 4 as a result, right?files
xyz1
xyz2
xyz3
The not so intelligent approach. Build up an ugly statement like this:
To easily get the 100
AND NOT EXISTS
lines, get all table names having a field namedfile_id
and concatenate them into SQL lines (replaceyour_database_name
with your actual database name):Copy the query result, add the lines to the first SQL query above, run it and you should have the desired files.
I am usually do like this in bash(maybe possible to make SQL, but I am not sure which table)
And you will see anything related to file_id in each one table.