I'm trying to create a complex SQL query (at least for me) but really don't know where to start.
Basically, I have a character object, and each character can be made of several parts. For example:
character table:
id | character
--------------
1 | 你
2 | 是
3 | 有
character_parts table:
id | character_id | part_id
---------------------------
1 | 1 | 4
2 | 1 | 9
3 | 1 | 5
4 | 2 | 2
5 | 2 | 34
6 | 2 | 43
7 | 3 | 21
8 | 3 | 16
9 | 3 | 41
10 | 3 | 43
So from that I know that:
Character 1 is made of parts 4, 9, 5
Character 2 is made of parts 2, 34, 43
Character 3 is made of parts 21, 16, 41, 43
Now what I would like to do is select all the characters that end by the specified parts.
For example, if I select all the characters that end by "16, 41, 43", I'll get Character 3. If I select all the characters that end by "43", I'll get Character 3 and 4.
I assume I need to build some query with subqueries, but not sure how to start, or if it can be done at all. So far I'm just selecting everything that include the required part IDs and then doing the comparison programmatically but this is too slow being I'm selecting way more than needed.
Any suggestion?