Foreign key is combined on 3 tables
I have a TableA table that contains 2 different foreign keys of the TableB and TableC tables. I am trying to get data from TableB, but depending on the value from TableC.
With this query I am getting all the values, but I don’t know how to write the where where to filter and get the data I want.
SELECT TableC.value1,TableB.value2
FROM TableA
JOIN TableB ON TableB.ID = TableA.ID
JOIN TableC ON TableC.ID = TableA.ID
Edit: Image of my tables.
So, I'm trying to show data from table B, but only those that match table C.
New edit:
I am getting values from 2 tables, but now I only need to show the column A value equal to "val2". Image that column B has values like "val2" "val2" "val3" and so on. I only need to show what is "val2".
source to share
You don't need to use a sentence WHERE
. You need to fix yours JOIN
and make it JOIN
tablec
with the condition TableA.TableC_ID = TableC.ID
, namely:
SELECT TableC.value1,TableB.value2
FROM TableA
JOIN TableB ON TableB.ID = TableA.ID
JOIN TableC ON TableA.TableC_ID = TableC.ID
WHERE tableA.ColumnA = 'val2';
source to share