简体   繁体   English

如何连接两个连接在一起的表,每个表都有两个其他表

[英]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在T2上链接,值为“498”。 T1 is linked on T3 with valude 383". T1在T3上与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在T4上链接,值为“383”。
T3 is linked on T5 with value 844". T3在T5上链接,值为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`

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

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