加入两个或多个没有公共字段的表

时间:2016-06-22 07:27:14

标签: sql sql-server sql-server-2008

我在数据库中有3个表

Project              Material Used            Document
------------        -------------------        ------------------------
PID |  Name         MID | PID |  Name          DocID | PID | Date
------------        -------------------        ------------------------
1   | A             1   |  1  |  A1            1     |  1  | 1/1/2016
2   | B             2   |  1  |  A1            2     |  1  | 1/2/2016
3   | C             3   |  1  |  A1            3     |  2  | 1/3/2016
                    4   |  2  |  A1            4     |  2  | 1/4/2016
                    5   |  2  |  A1            5     |  2  | 1/5/2016
                    6   |  3  |  A1            6     |  2  | 1/6/2016
                    7   |  3  |  A1            7     |  2  | 1/7/2016
                    8   |  3  |  A1            8     |  1  | 1/8/2016
                    9   |  3  |  A1            9     |  1  | 1/9/2016

如何查询下面的结果?

------------------------------------------------------------------------
PID     Project Name    MID             Material Name   DocID   Date
------------------------------------------------------------------------
1       A               1               A1              1       1/1/2016
1       A               2               A2              2       1/2/2016
1       A               3               A3              NULL    NULL
2       B               4               B1              3       1/3/2016
2       B               5               B2              4       1/4/2016
2       B               NULL            NULL            5       1/5/2016
2       B               NULL            NULL            6       1/6/2016
2       B               NULL            NULL            7       1/7/2016
3       C               6               C1              8       1/8/2016
3       C               7               C2              9       1/9/2016
3       C               8               C3              NULL    NULL
3       C               9               C4              NULL    NULL

材料和凭证表中的PID是外键。

我正在使用Microsoft SQL 2008。 有可能吗?

1 个答案:

答案 0 :(得分:1)

您似乎想要列中的列表。您可以使用full outer joinrow_number()

来获取此信息
select p.*, m.mid, m.name, d.docid, d.date 
from project p left join
     (select m.*, row_number() over (partition by pid order by mid) as seqnum
      from materials
     ) m
     on p.pid = m.pid full outer join
     (select d.*, row_number() over (partition by pid order by docid) as seqnum
      from documents
     ) d
     on p.pid =  d.pid and m.seqnum = d.seqnum;

嗯,试试这个版本:

select p.*, md.mid, md.name, md.docid, md.date 
from project p left join
     (select m.id, m.name, d.docid, d.date
      from (select m.*, row_number() over (partition by pid order by mid) as seqnum
            from materials
           ) m full outer join
           (select d.*, row_number() over (partition by pid order by docid) as seqnum
            from documents
           ) d
           on p.pid =  d.pid and m.seqnum = d.seqnum
      ) md
     on p.pid = md.pid;