【问题标题】:many to many joining多对多加入
【发布时间】:2012-10-07 10:44:06
【问题描述】:

您好,我目前正在学习 sql,我有 2 个一对多表,但我不明白如何将它们组合成多对多。我有一个连接表。

表父级 列父和父 ID

表子 列 child 和 childid

table parentchild --(join table selection parents with childid) 列 parentid 和 childid

这是我的代码。

select parent.name, parentchild.childid from parent
join parentchild
on parent.parentID = parentchild.parentid

select child.name, parentchild.parentid from child
join parentchild 
on child.childID = parentchild.childid

【问题讨论】:

    标签: asp.net sql sql-server sql-server-2012


    【解决方案1】:

    解决方案是编写一个查询,将parent 表连接到parentchild 表,然后将parentchild 表连接到child 表。

    select p.name, c.name 
    from parent p
    join parentchild pc
        on p.parentid = pc.parentid  -- joins parent to parentchild
    join child c
        on c.childID = pc.childid  -- joins parentchild to child
    

    【讨论】:

      猜你喜欢
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2013-03-25
      • 2013-06-17
      相关资源
      最近更新 更多