在同一个表上选择多个连接

时间:2014-09-18 09:53:03

标签: sql join sql-server-2008-r2 sql-server-2012

我在完成这项工作时遇到了问题:

我有这张桌子(项目):

ItemId   Name  
1        Test  
2        Foo  
3        Bar  
4        Etc
5        Some

每个项目都有几个链接到它的命令,在表CommandItem:

CommandId   ItemId  
912         2
916         2
922         2
954         2
976         3
977         3
979         3
982         3
998         4
1001        4
1020        4
1035        4
1039        5
1041        5

我想只获得几种commandId,匹配"功能"存储在表中的命令:

CommandId   FunctionId
912         81
916         72
922         56
954         13
976         81
977         72
979         56
982         13
998         81
1001        72
1020        56
1035        13
1039        81
1041        72

我只想要带有FunctionId 81,72或56的commandId 让我们说他们的名字是:
 81:黄色  72:布朗
 56:黑色

编辑:在某些情况下,itemId可能不存在某些命令 例如,我们可以说itemId 5有一个黄色和棕色的命令,但不是黑色的命令 在这种情况下,我仍然需要itemId 5出现在表中,其中" NULL"为黑色。

我想要的最终视图有这样的形式:

 ItemId      Name   Yellow  Brown  Black
 1           Test   NULL    NULL   NULL
 2           Foo    912     916    922
 3           Bar    976     977    979
 4           Etc    998     1001   1020
 5           Some   1039    1041   NULL

我试图做到这一点:

select 
    i.itemid, 
    i.name,
    c1.CommandId as brown, 
    c2.CommandId as yellow,
    c3.CommandId as black
from item i
inner join commanditem ci 
    on i.itemid = ci.itemid 
inner join command c1 
    on ci.CommandId = c1.CommandId and c1.FonctionId = 81
left outer  join command c2 
    on ci.CommandId = c2.CommandId and c2.FonctionId = 72
left outer  join command c3
    on ci.CommandId = c3.commandId and c3.FonctionId = 56

我也尝试使用内部连接,使用枢轴......但没有成功。

我希望我足够清楚,谢谢你能给我的任何帮助。

1 个答案:

答案 0 :(得分:1)

这对我有用:

select
    i.itemid,
    i.name,
    c1.CommandId as brown,
    c2.CommandId as yellow,
    c3.CommandId as black
from (items i left outer join (commanditem ci1 inner join command c1 on ci1.commandid = c1.commandid) on i.itemid = ci1.itemid
              left outer join (commanditem ci2 inner join command c2 on ci2.commandid = c2.commandid) on i.itemid = ci2.itemid
              left outer join (commanditem ci3 inner join command c3 on ci3.commandid = c3.commandid) on i.itemid = ci3.itemid)
where
      (c1.functionid = 81 or c1.functionid is null)
  and (c2.functionid = 72 or c2.functionid is null)
  and (c3.functionid = 56 or c3.functionid is null)

我在另一个DBMS上测试过它,但由于它是标准的SQL,它也可能对你有用。 结果是:

┌─────────────┬──────────┬─────────────┬─────────────┬─────────────┐
│itemid       │name      │brown        │yellow       │black        │
├─────────────┼──────────┼─────────────┼─────────────┼─────────────┤
│            2│Foo       │          912│          916│          922│
│            3│Bar       │          976│          977│          979│
│            4│Etc       │          998│         1001│         1020│
│            1│Test      │             │             │             │
└─────────────┴──────────┴─────────────┴─────────────┴─────────────┘
(4 rows)

好的,我明白了。下一个查询似乎做了正确的事情:

select
    i.itemid,
    i.name,
    c1.CommandId as brown,
    c2.CommandId as yellow,
    c3.CommandId as black
from (items i left outer join (commanditem ci1 inner join command c1 on ci1.commandid = c1.commandid and c1.functionid = 81) on i.itemid = ci1.itemid
              left outer join (commanditem ci2 inner join command c2 on ci2.commandid = c2.commandid and c2.functionid = 72) on i.itemid = ci2.itemid
              left outer join (commanditem ci3 inner join command c3 on ci3.commandid = c3.commandid and c3.functionid = 56) on i.itemid = ci3.itemid)

使用新数据,它会产生所需的结果:

┌─────────────┬──────────┬─────────────┬─────────────┬─────────────┐
│itemid       │name      │brown        │yellow       │black        │
├─────────────┼──────────┼─────────────┼─────────────┼─────────────┤
│            1│Test      │             │             │             │
│            2│Foo       │          912│          916│          922│
│            3│Bar       │          976│          977│          979│
│            4│Etc       │          998│         1001│         1020│
│            5│Some      │         1039│         1041│             │
└─────────────┴──────────┴─────────────┴─────────────┴─────────────┘
(5 rows)