[英]querying many-to-many relationship on 3 table
I have 3 table 我有3张桌子
Partcar
BarbellWheels
Assembly
now, im confused how to manage this table to got connection, cause Partcar
and Assembly
is many-to-many relationship. 现在,我很困惑如何管理该表进行连接,导致Partcar
与Assembly
是多对多的关系。 But, BarbellWheels
will merging with Partcar
in Assembly
. 但是, BarbellWheels
将在Assembly
与Partcar
合并。 How to make this 3 table have many-to-many relationship, Partcar
and BarbellWheels
with Assembly
. 如何使这3个表具有多对多关系, Partcar
和BarbellWheels
与Assembly
。 And if it can, 3 table in 1, what a query to join them? 如果可以的话,请在1中的3个表中加入一个查询?
So far, what im thinking is, BarbellWheels
join to Partcar
1st and then join to Assembly
. 到目前为止,我的想法是, BarbellWheels
加入Partcar
1st,然后加入Assembly
。
any advise? 有什么建议吗?
EDIT : 编辑:
There is 5 data on Partcar and must combine to 1 in Assembly and BarbellWheels will combine in Assembly with max 2 data on it. Partcar上有5个数据,必须在Assembly中合并为1,而BarbellWheels在Assembly中将合并有最多2个数据。
EDIT EDITED : im just relize i didnt mention the relationship. 编辑:即时通讯,我没有提到我的关系。 here's the code. 这是代码。
CREATE TABLE `Partcar` (
`partcarID` Char(7) NOT NULL,
`partcar_price` decimal(19,4) DEFAULT NULL,
`partcar_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`PartcarID`)
);
ALTER TABLE `assembly` (
`assemblyID` char(8) NOT NULL,
`assembly_price` decimal(19,4) DEFAULT NULL,
`partcarIDFK` int(10) NOT NULL,
`barbellIDFK` int(10) NOT NULL,
PRIMARY KEY (`assemblyID`),
UNIQUE KEY `partcar` (`partcarIDFK`),
UNIQUE KEY `barbell` (`barbellIDFK`),
CONSTRAINT `partcarIDFK` FOREIGN KEY (`partcarIDFK`) REFERENCES partcar(`partcarID`),
CONSTRAINT barbellIDFK FOREIGN KEY (`barbellIDFK`) REFERENCES barbell(`barbellID`)
);
ALTER TABLE `barbell` (
`barbellID` char(10) NOT NULL,
`name_barbell` varchar(100) DEFAULT NULL,
`carIDFK` int(10) NOT NULL,
PRIMARY KEY (`barbellID`),
UNIQUE KEY `car` (`carID`),
Consider the following: 考虑以下:
Recipes 菜谱
recipe_id recipe
1 cheese on toast
2 macaroni and cheese
Ingredients 配料
ingredient_id ingredient
101 cheese
102 toast
103 macaroni
Recipe_Ingredient 配方成分
recipe_id ingredient_id
1 101
1 102
2 103
2 104
Same cases in below url,Find the solution also. 网址下的情况相同,也请查找解决方案。
https://www.codeproject.com/Questions/383844/Sql-Queries-to-show-contents-of-the-table-many-to https://www.codeproject.com/Questions/383844/Sql-Queries-to-show-contents-of-the-table-many-to
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.