MS Access SQL组合了多个表

时间:2018-03-20 05:14:31

标签: sql database ms-access ms-access-2016

我在MS Access 2016中有两个表,如下所示:

表:的表1

         
Column1 | Column2 | Column3 | Column4
----------------------------------------
   1    |    A    |      P   |   S
   1    |    B    |      J   |   J
   1    |    C    |      P   |   S
   1    |    D    |      S   |   P
   2    |    E    |      S   |   P
   2    |    F    |      P   |   S
   2    |    G    |      P   |   S
   2    |    H    |      S   |   P
   2    |    K    |      J   |   J

表:表2


Column5 |   Column6 |   Column7 |   Column8
--------------------------------------------------
    1   |       Z   |       i   |   Supplier
    1   |       Z   |       ii  |   Supplier
    1   |       Z   |       iii |   Supplier
    1   |       Z   |       iv  |   Supplier
    1   |       Z   |       v   |   Supplier
    1   |       Z   |       vi  |   Joint
    1   |       Z   |       vii |   Joint
    1   |       Y   |       viii|   Joint
    1   |       Y   |       ix  |   Supplier
    1   |       L   |       x   |   Supplier
    1   |       L   |       xi  |   Supplier
    1   |       L   |       xii |   Supplier
    2   |       W   |       xx  |   Joint
    2   |       W   |       xxi |   Joint
    2   |       W   |       xxii|   Joint
    2   |       W   |      xxiii|   Joint
    2   |       W   |      xxiv |   Joint
    2   |       M   |       xv  |   Supplier

有没有办法合并表格,如下所示。行不应该重复。如果表2中存在单元格而不是1,则将结果保留为结果

查询结果:


Column1 |   Column2 |   Column3 |   Column4 |   Column5 |   Column6 |   Column7 |   Column8
------------------------------------------------------------------------------------------

    1   |   A       |       P   |       S   |       1   |       Z   |       i   |Supplier
    1   |   B       |       J   |       J   |       1   |       Z   |       ii  |Supplier
    1   |   C       |       P   |       S   |       1   |       Z   |       iii |Supplier
    1   |   D       |       S   |       P   |       1   |       Z   |       iv  |Supplier
    1   |           |           |           |       1   |       Z   |       v   |Supplier
    1   |           |           |           |       1   |       Z   |       vi  |Joint
    1   |           |           |           |       1   |       Z   |       vii |Joint
    1   |           |           |           |       1   |       Y   |       viii|Joint
    1   |           |           |           |       1   |       Y   |       ix  |Supplier
    1   |           |           |           |       1   |       L   |       x   |Supplier
    1   |           |           |           |       1   |       L   |       xi  |Supplier
    1   |           |           |           |       1   |       L   |       xii |Supplier
    2   |   E       |       S   |       P   |       2   |       W   |       xx  |Joint
    2   |   F       |       P   |       S   |       2   |       W   |       xxi |Joint
    2   |   G       |       P   |       S   |       2   |       W   |       xxii|Joint
    2   |   H       |       S   |       P   |       2   |       W   |     xxiii |Joint
    2   |   K       |       J   |       J   |       2   |       W   |       xxiv|Joint
    2   |           |           |           |       2   |       M   |       xv  |Supplier

1 个答案:

答案 0 :(得分:0)

以下是您的解决方案:

SELECT Column5 AS Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8 
FROM 
     (SELECT Column1,Column2,Column3,Column4,
      @rownumber:=IF(@lastId=Column1,@rownumber+1,1) AS RowNumber,
      @lastId:=Column1
      FROM Table1,
      (Select @rownumber:=0,@lastId:=MIN(Column1) FROM TABLE1 ) AS r) AS Tbl1
RIGHT JOIN 
      (SELECT Column5,Column6,Column7,Column8,
       @rownumber:=IF(@lastId=Column5,@rownumber+1,1) AS RowNumber,
       @lastId:=Column5
       FROM Table2, 
       (Select @rownumber:=0,@lastId:=MIN(Column5) FROM TABLE2 ) AS r) AS Tbl2
ON Tbl1.Column1 = Tbl2.Column5
AND Tbl1.RowNumber = Tbl2.RowNumber

按照以下链接进行演示:

  

http://sqlfiddle.com/#!9/3bcd6/25