我在Microsoft Access 2013中有两个表:
tblService
PART_ID SERV_DATE SERV_REMARK A0001 11/1/2013 GOOD#1 A0001 11/13/2013 GOOD#2 A0001 11/25/2013 GOOD#3
tblWithdraw
PART_ID DRAWN_DATE DRAWN_REASON DRAWN_TO A0001 11/6/2013 PM OW601 A0001 11/20/2013 120 PM OW603 A0001 11/30/2013 REPLACEMENT OW605
这是我想要的结果:
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
但我无法使用INNER JOIN
,RIGHT JOIN
或LEFT JOIN
获得结果。我尝试使用FULL JOIN
,但系统提示我错误:
FROM子句中的语法错误
以下是我的SQL。
SELECT
S.PART_ID, SERV_DATE,
SERV_REMARK, DRAWN_DATE,
DRAWN_REASON, DRAWN_TO
FROM
tblService AS S
RIGHT 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/13/2013 GOOD#2 11/6/2013 PM OW601 A0001 11/25/2013 GOOD#3 11/6/2013 PM OW601 A0001 11/1/2013 GOOD#1 11/20/2013 120 PM OW603 A0001 11/13/2013 GOOD#2 11/20/2013 120 PM OW603 A0001 11/25/2013 GOOD#3 11/20/2013 120 PM OW603 A0001 11/1/2013 GOOD#1 11/30/2013 REPLACEMENT OW605 A0001 11/13/2013 GOOD#2 11/30/2013 REPLACEMENT OW605 A0001 11/25/2013 GOOD#3 11/30/2013 REPLACEMENT OW605
有人可以更正我的SQL吗?或任何正确代码的链接? THX!
答案 0 :(得分:0)
您的两张桌子之间没有独特的关系,而您需要一张。例如,您必须重新构建表格(以及如何输入数据),以便它们具有一个值,使某个部分的每个实例与其他具有相同PART_ID
的个体实例相同。
例如:
UID 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
UID 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
然后您可以将SQL修改为:
SELECT
S.PART_ID, SERV_DATE,
SERV_REMARK, DRAWN_DATE,
DRAWN_REASON, DRAWN_TO
FROM
tblService AS S
RIGHT JOIN
tblWithdraw AS W
ON
S.UID=W.UID;
这将给出您正在寻找的结果。