外键连接在3个表上。

时间:2020-12-31 11:28:40

I have a table TableA that holds 2 different foreign keys of tables TableB and TableC. I'm trying to get the data from table TableB but depending on value from table TableC.

我有一个表表表,它包含两个表表表和表的外键。我试图从表b中获取数据但这取决于表TableC的值。

With this query I get all the values but I don't know how to write where clause to filter and get data that I need.

通过这个查询,我得到了所有的值,但是我不知道如何编写where子句来过滤和获取我需要的数据。

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

Edit : Picture of my tables. 外键连接在3个表上。

编辑:我的桌子的图片。

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

我试图显示表B中的数据,但只显示表C中的数据。

New edit : 外键连接在3个表上。

新编辑:

I get the values from 2 tables,but now I need to show only column A value that is equal to "val2". Image that column B have values as "val2" "val2" "val3" and so on. I need to show only that which is "val2".

我从两个表中得到值,但是现在我只需要显示列A值,它等于“val2”。假设列B的值为“val2”“val2”“val3”等等。我只需要显示“val2”。

2 个解决方案

#1


1  

You don't have to use WHERE clause. What you need is fix your JOIN and make it JOIN the tablec with the condition TableA.TableC_ID = TableC.ID instead, like this:

你不需要使用WHERE子句。您需要的是修复您的连接,并使它与条件表相连接。TableC_ID = TableC。ID相反,像这样:

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

#2


3  

I'm not sure whether I get your question right, but shouldn't the following 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

#1


1  

You don't have to use WHERE clause. What you need is fix your JOIN and make it JOIN the tablec with the condition TableA.TableC_ID = TableC.ID instead, like this:

你不需要使用WHERE子句。您需要的是修复您的连接,并使它与条件表相连接。TableC_ID = TableC。ID相反,像这样:

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

#2


3  

I'm not sure whether I get your question right, but shouldn't the following 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