i am writing an application and search for a specific select statement. The situation in my MYSQL database is the following:
- Each entry can have several units. One unit is related to exactly one entry.
- Each unit can have several subunits. One subunit can be related to several units.
I realised this situation with the following tables:
- entry: id (int)
- unit: id (int), entry_id (int)
- unit_subunits: unit_id (int), subunit_id (int)
- subunit: id (int)
What I want now is the following: I want that the user inputs a subunit.id and the gets a list with all entry.ids, which contain the input subunit.id, as well as all other subunit.ids, which are related to those found entry.ids. The result should looke like:
(input = subunit 5)
entry.id | subunit.id
entry 1 | subunit 5
entry 1 | subunit 26
entry 2 | subunit 2
entry 2 | subunit 5
...
To only get the entry.ids, which are related to the input subunit.id, was easy. But I falied to get eveything together into one SQL statement. I solved it in my application, by creating two different statements (whereby the second statement is executed for each found entry.id). Is there an elegant solution, to put this all into one sql statement?
Thank you very much!
UPDATE 1
To avoid misunderstandings like in the comment to this question: There is actually a dataset. But in order to make the question as theoretical, general and comprehensible as possible, i reduced it only to "ids".