I'm working with an old database and I want to select data spread across different tables but in an recurring manner based on multiple values in one cell.
There are three tables (here with fictional data): treaties, parties, related_parties. The treaties table includes information about specific treaties as well as the ID of the parties that signed it. The parties table comprises information about the parties, whereas the related_parties table consists of information about parties that were related (the related_partner_id being the IDs used the parties table).
Table 1: treaties
id | name | party_id |
---|---|---|
1 | Peace of Westphalia | 49, 80 |
2 | Peace of Rijswijk | 49, 50, 81 |
Table 2: parties
party_id | party_name |
---|---|
49 | Holy Roman Empire |
50 | Dutch Republic |
51 | Mainz |
52 | Cologne |
80 | France |
81 | Sweden |
82 | Paris |
83 | Bordeaux |
Table 3: related_parties
party_id | related_party_id |
---|---|
49 | 51, 52 |
80 | 82, 83 |
What I want as the output is something like this, where information about the parties is gathered for every value in the relevant cells. So for the first treaty (1) this would be:
id | name | party_id | party_name | related_party_id | related_party_name |
---|---|---|---|---|---|
1 | Peace of Westphalia | 49 | Holy Roman Empire | 51 | Mainz |
1 | Peace of Westphalia | 49 | Holy Roman Empire | 52 | Cologne |
1 | Peace of Westphalia | 80 | France | 82 | Paris |
1 | Peace of Westphalia | 80 | France | 83 | Bordeaux |
Is this at all doable? So far every query I've created only retrieves the data pertaining to the first value in a cell.