简体   繁体   English

连接 3 个表,其中两个有主键,第三个有两个外键

[英]Join 3 tables two have primary key and third one have both foreign keys

I have three table A, B, C,我有三个表A,B,C,

Table A is having primary_key a.id_attr and other column is a.id_attr_group.表 A 有 primary_key a.id_attr,其他列是 a.id_attr_group。

Table B has one primary key b.id_prod_attr and other column is b.id_prod表 B 有一个主键 b.id_prod_attr,另一列是 b.id_prod

Table C表 C

And Third and last one is combination of both keys c.id_attr and c.id_prod_attr in table c their could be multiple entries having 2-3 same rows from both table.第三个也是最后一个是表 c 中 c.id_attr 和 c.id_prod_attr 两个键的组合,它们可能是两个表中具有 2-3 个相同行的多个条目。

I want to get unique rows those fulfill the condition a.id_attr_group=2 and b.id_prod=1 and those both condition matches in Table C [combinations] I have tried the Inner join, LEFT OUTER JOIN Subquery but no use not able to get the exact results.我想获得满足条件 a.id_attr_group=2 和 b.id_prod=1 以及表 C [组合] 中的两个条件匹配的唯一行确切的结果。

Thanks in advance if someone could help me out?如果有人可以帮助我,请提前致谢?

Wait so you have等你有

A一种

id_attr [PK] id_attr [PK]
id_attr_group [UNIQUE] (presumably) id_attr_group [唯一](大概)

B

id_prod_attr [PK] id_prod_attr [PK]
id_prod [UNIQUE] (presumably) id_prod [唯一](大概)

C C

#id_attr [FK from A] #id_attr [来自 A 的 FK]
#id_prod_attr [FK from B] #id_prod_attr [来自 B 的 FK]

and then I am not really sure about what you're trying to do.然后我不太确定你想要做什么。

can't you just do你不能只是做

SELECT *选择 *
FROM C INNER JOIN B ON B.id_prod_attr = C.id_prod_attr从 C INNER JOIN B ON B.id_prod_attr = C.id_prod_attr
INNER JOIN A ON A.id_attr = C.id_attr INNER JOIN A ON A.id_attr = C.id_attr
WHERE A.id_attr_group = 2 and B.id_prod = 1其中 A.id_attr_group = 2 和 B.id_prod = 1
? ?

select *

from HomeLoan  -- this table  has the foreign keys
inner join Home on Home.homeNumber = HomeLoan.homeNumber
inner join HomeOwner on HomeOwner.ownerNID = HomeLoan.ownerNID  --3 table join

Where the 3 tables you are joining are HomeLoan , Home and HomeOwner .您要加入的 3 个表是HomeLoanHomeHomeOwner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM