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. enter image description here

So, I'm trying to show data from table B, but only those that match table C.

New edit: enter image description here

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".

+3


source to share


2 answers


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';

      

+1


source


I'm not sure if I got your question right, but shouldn't the next one work?



SELECT TableC.value1,TableB.value2 
FROM TableA 
JOIN TableB ON TableB.ID = TableA.ID 
JOIN TableC ON TableC.ID = TableA.ID 
WHERE TableC.myColumn = MyValue

      

+3


source







All Articles