简体   繁体   中英

querying many-to-many relationship on 3 table

I have 3 table

  1. Partcar
  2. BarbellWheels
  3. Assembly

now, im confused how to manage this table to got connection, cause Partcar and Assembly is many-to-many relationship. But, BarbellWheels will merging with Partcar in Assembly . How to make this 3 table have many-to-many relationship, Partcar and BarbellWheels with Assembly . And if it can, 3 table in 1, what a query to join them?

So far, what im thinking is, BarbellWheels join to Partcar 1st and then join to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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