当一个 table 有多个对应值时连接两个 table

Joining two tables when one table has multiple corresponding values

对于某些背景,我拥有的是一个填充数据库的订购系统。数据库中有两个table,order_Parent,order_Children.

order_Parent 包含更广泛的信息,例如 ID、shippingDesination、createdBy、highPriority。还有更多列,但我关注的是 order_Parent.

的 ID 列和 highPriority

当查看 order_children table 时,可以有多个 children 到一个 parent 订单。 children 是订单的各个部分,一些列是 itemDescription、数量、优先级、linkID。 同样还有更多列,但对于 order_Children table 我只担心 linkID 列和优先级列。

举个例子,假设 John Smith 订购了 3 个螺钉、2 个螺母和 1 个螺栓。这些都可以在优先级列中具有不同的优先级。我担心的是如果它是优先级1,这意味着我需要加急订单。

因此,在完成该订单后,tables 将如下所示:

order_Parent:
编号:15
送货目的地:123 Road St
创建者:约翰·史密斯
高优先级:空

order_Children:
项目描述:螺丝
数量:3
优先级:1
链接ID:15

物品描述:坚果
数量:2
优先级:0
链接ID:15

物品描述:螺栓
数量:1
优先级:0
链接ID:15

所以在这里我们可以看到 children 可以有不同的优先级。然后我想做的是将 order_Parent highPriority 行更新为 1,如果任何对应的 children 具有优先级 1.

我想到的是以linkID和ID为共同点将table拼接在一起。 但是当有多个 child 实例与 parent.

具有相同 ID 时,我不知道如何加入

完成 table 后,我相信下一步是查看 child_Order 优先级,如果有 1,则使用 UPDATE 函数更新 child_Order 中的相应行=60=] table.

这是我目前所拥有的,但是 returns 行太多,很多行似乎是重复的,但我不确定为什么,因为我刚刚开始 SQL。我也没有将 UPDATE 函数添加到查询中,但很想知道进入哪个方向。


    SELECT Parent.ID AS ID
    ,Parent.HighPriority AS HighPriority
    ,order_Children.Priority AS Priority
    
    FROM order_Parent, order_Children
    INNER JOIN order_parent AS Parent ON Parent.ID = order_Children.linkID

此语法将创建一个 CROSS JOIN

FROM order_Parent, order_Children

Another 交叉连接的好例子。

您正在寻找的语法

SELECT Parent.ID AS ID
    ,Parent.HighPriority AS HighPriority
    ,order_Children.Priority AS Priority
    FROM order_Children
    INNER JOIN order_parent AS Parent ON Parent.ID = order_Children.linkID