如何联接两个表,每个表又与其他两个表联接在一起

时间:2019-04-28 15:42:52

标签: php mysql join

我已经抬头了一段时间。 我希望进行一个查询,该查询将两个表连接在一起,每个表都与以下两个表连接在一起,如下所示。 我知道如何联接表,但现在我阻止了。

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

表格示例

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在T2上链接为值“ 498”。 T1在T3上链接着值383“。

同时:

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在T4上链接为值“ 383”。
T3在T5上链接的值为844“。

1 个答案:

答案 0 :(得分:0)

我找到的解决方案:

`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`