加入两个表并显示所有记录(包括null)

时间:2013-11-18 01:56:57

标签: ms-access ms-access-2013

我在Microsoft Access 2013中有两个表:

tblService

ID PART_ID SERV_DATE SERV_REMARK
1   A0001  11/1/2013   GOOD#1
2   A0001  11/13/2013  GOOD#2
3   A0001  11/25/2013  GOOD#3
4   B0001  11/26/2013  GOOD#4
5   C0001  12/1/2013   GOOD#5
6   C0001  12/10/2013  GOOD#6
7   C0001  12/20/2013  GOOD#7

tblWithdraw

ID PART_ID DRAWN_DATE DRAWN_REASON DRAWN_TO
1   A0001  11/6/2013       PM        OW601
2   A0001  11/20/2013    120 PM      OW603
3   A0001  11/30/2013  REPLACEMENT   OW605 
4   C0001  12/2/2013      30 PM      OW701
5   C0001  12/15/2013    180 PM      OW702

我想加入这两个表,得到如下所示的结果:

PART_ID SERV_DATE SERV_REMARK DRAWN_DATE DRAWN_REASON DRAWN_TO
 A0001  11/1/2013   GOOD#1    11/6/2013       PM        OW601
 A0001  11/13/2013  GOOD#2    11/20/2013    120 PM      OW603
 A0001  11/25/2013  GOOD#3    11/30/2013  REPLACEMENT   OW605
 B0001  11/26/2013  GOOD#4
 C0001  12/1/2013   GOOD#5    12/2/2013       PM        OW701
 C0001  12/10/2013  GOOD#6    12/15/2013    180 PM      OW702
 C0001  12/20/2013  GOOD#7

我一直 working for weeks ,但仍然无法获得结果。 有人可以给我看看查询吗?非常感谢!

编辑11月18日1035:

我试过了:

  

SELECT s.PART_ID,s.SERV_DATE,s.SERV_REMARK,
  w.DRAWN_DATE,w.DRAWN_REASON,w.DRAWN_TO
  来自tblService AS的   LEFT JOIN tblWithdraw AS w
  ON s.PART_ID = w.PART_ID

结果是:

PART_ID SERV_DATE   SERV_REMARK DRAWN_DATE  DRAWN_REASON    DRAWN_TO
 A0001  11/1/2013     GOOD #1   11/6/2013        PM           OW601
 A0001  11/1/2013     GOOD #1   11/20/2013     120 PM         OW602
 A0001  11/1/2013     GOOD #1   11/30/2013  REPLACEMENT       OW603
 A0001  11/13/2013    GOOD #2   11/6/2013        PM           OW601
 A0001  11/13/2013    GOOD #2   11/20/2013     120 PM         OW602
 A0001  11/13/2013    GOOD #2   11/30/2013  REPLACEMENT       OW603
 A0001  11/25/2013    GOOD #3   11/6/2013        PM           OW601
 A0001  11/25/2013    GOOD #3   11/20/2013     120 PM         OW602
 A0001  11/25/2013    GOOD #3   11/30/2013  REPLACEMENT       OW603
 B0001  11/26/2013    GOOD #4           
 C0001  12/1/2013     GOOD #5   12/2/2013        PM           OW701
 C0001  12/1/2013     GOOD #5   12/15/2013     180 PM         OW702
 C0001  12/10/2013    GOOD #6   12/2/2013        PM           OW701
 C0001  12/10/2013    GOOD #6   12/15/2013     180 PM         OW702
 C0001  12/20/2013    GOOD #7   12/2/2013        PM           OW701
 C0001  12/20/2013    GOOD #7   12/15/2013     180 PM         OW702

3 个答案:

答案 0 :(得分:3)

使用相关子查询为DRAWN_DATEPART_ID的每个组合获取下一个SERV_DATE。在Access 2007中使用示例数据,这是以下查询的输出。

PART_ID SERV_DATE  SERV_REMARK next_drawn_date
A0001    11/1/2013 GOOD#1            11/6/2013
A0001   11/13/2013 GOOD#2           11/20/2013
A0001   11/25/2013 GOOD#3           11/30/2013
B0001   11/26/2013 GOOD#4 
C0001    12/1/2013 GOOD#5            12/2/2013
C0001   12/10/2013 GOOD#6           12/15/2013
C0001   12/20/2013 GOOD#7 

SELECT
    s.PART_ID,
    s.SERV_DATE,
    s.SERV_REMARK,
    (
        SELECT Min(DRAWN_DATE)
        FROM tblWithDraw
        WHERE
                PART_ID = s.PART_ID
            AND DRAWN_DATE >= s.SERV_DATE
    ) AS next_drawn_date
FROM tblService AS s;

然后您可以将其用作子查询并将其加入tblWithdraw

SELECT
    sub.PART_ID,
    sub.SERV_DATE,
    sub.SERV_REMARK,
    sub.next_drawn_date AS DRAWN_DATE,
    w.DRAWN_REASON,
    w.DRAWN_TO
FROM
    (
        SELECT
            s.PART_ID,
            s.SERV_DATE,
            s.SERV_REMARK,
            (
                SELECT Min(DRAWN_DATE)
                FROM tblWithDraw
                WHERE
                        PART_ID = s.PART_ID
                    AND DRAWN_DATE >= s.SERV_DATE
            ) AS next_drawn_date
        FROM tblService AS s
    ) AS sub
    LEFT JOIN tblWithdraw AS w
    ON
            (sub.PART_ID = w.PART_ID)
        AND (sub.next_drawn_date = w.DRAWN_DATE);

答案 1 :(得分:1)

我原本认为以下内容适合您:

SELECT s.PART_ID, s.SERV_DATE, s.SERV_REMARK, 
  w.DRAWN_DATE, w.DRAWN_REASON, w.DRAWN_TO
FROM tblService AS s
  LEFT JOIN tblWithdraw AS w
  ON s.PART_ID = w.PART_ID

然而,这不起作用,因为部件ID在tblService表中不是唯一的。然后我开始寻找两个表中匹配的日期字段。日期字段不匹配。

因此,根据您在此处显示的内容,您无法将两个表链接在一起。

您的输出看起来像是想要遍历tblService表,然后在tblWithdraw表中找到匹配的记录并将它们匹配在一起。然后,如果PART_ID表中存在具有相同tblService的第二条记录,请将其与PART_ID表中第二次出现的相同tblWithdraw匹配。然后多次重复此操作。如果tblWithdraw中的匹配记录与n中的tblService匹配匹配,则无论如何都会输出tblService中找到的信息。

您无法在SQL中执行这样的复杂查询。 如果您真的想这样做,可以使用VBA,C#和/或等代码来迭代tblService然后遍历tblWithdraw以查找您要查找的内容。

更好的选择:如果您显示的表中只包含您要显示的字段,则需要修改表并向tblService添加唯一的主键字段。然后在tblWithdraw表中添加一个与tblService主键匹配的外键。

然后以下内容将起作用:

SELECT s.PART_ID, s.SERV_DATE, s.SERV_REMARK, 
  w.DRAWN_DATE, w.DRAWN_REASON, w.DRAWN_TO
FROM tblService AS s
  LEFT JOIN tblWithdraw AS w
  ON s.tblServiceID = w.tblServiceID 

答案 2 :(得分:0)

SELECT s.PART_ID,s.SERV_DATE,s.SERV_REMARK,   w.DRAWN_DATE,w.DRAWN_REASON,w.DRAWN_TO 来自tblService AS   LEFT JOIN tblWithdraw AS w   ON s.tblServiceID = w。“TWLWITHDRAW” - 小修正