MS Access SQL 组合多个表

Posted

技术标签:

【中文标题】MS Access SQL 组合多个表【英文标题】:MS Access SQL combine multiple tables 【发布时间】:2018-03-20 05:14:31 【问题描述】:

我在 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

表格:Table2


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

有没有办法合并表格,如下所示。行不应重复。如果 Table2 中存在单元格而不是 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】:

这是您的解决方案:

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

【讨论】:

以上是关于MS Access SQL 组合多个表的主要内容,如果未能解决你的问题,请参考以下文章

MS Access:从 SQL 传递组合框值

MS Access SQL 查询 - 将所有表与多个表联合 - 重复字段

MS Access - 构建一个组合框,该组合框根据先前的组合框选择返回多个值

MS Access SQL 多个 JOIN 和 WHERE 子句

在 Ms-Access 中将表名作为查询参数传递

3 表连接的 MS Access VB SQL 语法