简体   繁体   中英

How to join two tables that are joined together with two other tables each

I've been taking my head for a while. I wish to make a query that joins two tables, each of these tables and joined with two other tables as the schema below. I know how to join tables but at this point I block.

SELECT

 recipe_requirement.ID recipe_requirement_ID,
 recipe_requirement.RecipeID recipe_requirement_RecipeID,
 recipe_requirement.MaterialObjectTypeID,
 recipe_requirement_MaterialObjectTypeID,
 recipe_requirement.Quantity recipe_requirement_Quantity,    
 recipe_requirement.IsRegionItemRequired,
 recipe_requirement_IsRegionItemRequired,
 recipe.ID recipe_ID,
 recipe.Name recipe_Name,
 recipe.StartingToolsID recipe_StartingToolsID,
 items.ID items_ID,
 items.ContainerID items_ContainerID,
 items.ObjectTypeID items_ObjectTypeID,
 items.Quantity items_Quantity,
 items.FeatureID items_FeatureID,
 objects_types.ID objects_types_ID,
 objects_types.Name objects_types_Name,
 movable_objects.ID movable_objects_ID,
 movable_objects.ObjectTypeID movable_objects_ObjectTypeID,
 movable_objects.RootContainerID movable_objects_RootContainerID,
 movable_objects.IsComplete movable_objects_IsComplete,
 movable_objects.CustomNameId movable_objects_CustomNameId

FROM recipe_requirement

JOIN movable_objects ON movable_objects.RootContainerID = items.ContainerID

JOIN objects_types ON objects_types.ID = items.ObjectTypeID

JOIN recipe ON recipe.ID = recipe_requirement.RecipeID

JOIN items ON items.ObjectTypeID = recipe_requirement.MaterialObjectTypeID

JOIN objects_types ON objects_types.ID = Recipe_requirement.MaterialObjectTypeID

WHERE movable_objects.IsComplete = 1

Example of table

T1 : recipe_requirement 
ID  1708
RecipeID    498
MaterialObjectTypeID    383
Quantity    1
IsRegionItemRequired    0

T2 - recipe 
ID  498
Name    Beef Stew
StartingToolsID 1054

T3 - items  
ID  5780
ContainerID 844
ObjectTypeID    383
Quantity    357
FeatureID   0

T1 is linked on T2 with value "498". T1 is linked on T3 with valude 383".

And in the same time :

T3 - items  
ID  5780
ContainerID 844
ObjectTypeID    383
Quantity    357
FeatureID   0

T4 - objects_types  
ID  383
Name    Beef

T5 - movable_objects    
ID  728
ObjectTypeID    104
RootContainerID 844
IsComplete  1
CustomNameId    4

T3 is linked on T4 with value "383".
T3 is linked on T5 with value 844".

Solution i found :

`SELECT
    recipe_requirement.ID recipe_requirement_ID,
    recipe_requirement.RecipeID recipe_requirement_RecipeID,
    recipe_requirement.MaterialObjectTypeID recipe_requirement_MaterialObjectTypeID,
    recipe_requirement.Quantity recipe_requirement_Quantity,
    recipe_requirement.IsRegionItemRequired recipe_requirement_IsRegionItemRequired,
    recipe.ID recipe_ID,
    recipe.Name recipe_Name,
    recipe.StartingToolsID recipe_StartingToolsID,
    items.ID items_ID,
    items.ContainerID items_ContainerID,
    items.ObjectTypeID items_ObjectTypeID,
    items.Quantity items_Quantity,
    items.FeatureID items_FeatureID,
    objects_types.ID objects_types_ID,
    objects_types.Name objects_types_Name,
    movable_objects.ID movable_objects_ID,
    movable_objects.ObjectTypeID movable_objects_ObjectTypeID,
    movable_objects.RootContainerID movable_objects_RootContainerID,
    movable_objects.IsComplete movable_objects_IsComplete,
    movable_objects.CustomNameId movable_objects_CustomNameId
FROM recipe_requirement
JOIN recipe ON recipe.ID = recipe_requirement.RecipeID
JOIN (items
JOIN objects_types ON objects_types.ID = items.ObjectTypeID
JOIN movable_objects ON movable_objects.RootContainerID = items.ContainerID
) ON items.ObjectTypeID = recipe_requirement.MaterialObjectTypeID`

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