I understand the basic concept of INNER JOINs but I'm having trouble with this particular case (the following three tables, PK = Primary Key, FK = Foreign Key)-:
tbl_goal_context (many-to-many table)
rowId(PK) | goalRowId(FK) | contextRowId (FK) |
1 | 2 | 5 |
tbl_context_items
rowId(PK) | ... |
5 | ... |
tbl_context_categories_items (many-to-many table)
rowId(PK) | catRowId(FK) | itemRowId(FK) |
1 | 3 | 5 |
I'm trying to list rows from tbl_context_items that are connected via the two many-to-many tables where catRowId and goalRowId are known.
So, for example, let's say I want to pull all the rows from tbl_context_items that are connected to goalRowId = 2
and catRowId = 3
.
Here is my attempt (using the example numbers above), which is giving me the error: "Error: ambiguous column name: tbl_goal_context.goalRowId Unable to execute statement"
SELECT tbl_context_categories_items.itemRowId, tbl_context_categories_items.catRowId, tbl_goal_context.goalRowId, tbl_goal_context.contextRowId, tbl_context_items.rowId AS rowId, tbl_context_items.shortText AS shortText, tbl_context_items.userMade AS userMade
FROM tbl_context_categories_items
INNER JOIN tbl_goal_context ON tbl_goal_context.contextRowId = tbl_context_items.rowId
INNER JOIN tbl_goal_context ON tbl_context_categories_items.itemRowId = tbl_context_items.rowId
WHERE tbl_context_categories_items.catRowId = 3
AND tbl_goal_context.goalRowId = 2
ORDER BY tbl_context_items.shortText ASC
Are INNER JOINs not fully appropriate here? Do I need a sub-query?