MS Access 完全外连接中出现莫名其妙的行为?

发布于 2024-11-30 22:30:04 字数 945 浏览 1 评论 0原文

我尝试在 MS Access 中对两个表 CMVSCTOXOSC 进行完全外部联接,以检查两个表/其中一个表中包含哪些 ID:

(SELECT * FROM CMVSC LEFT JOIN TOXOSC ON CMVSC.ID = TOXOSC.ID)
UNION 
(SELECT * FROM TOXOSC LEFT JOIN CMVSC ON TOXOSC.ID = CMVSC.ID);

查询可以执行,但没有达到我的预期。

CMVSC 中的前几条记录(按 ID 升序排序)为:

ID      CMV_NEGDATE CMV_POSDATE
10245   04.02.2010  28.06.1999
10642   08.10.1998  05.09.1991

TOXOSC 中的前几条记录(按 ID 升序排序)为:

ID      TOXO_NEGDATE TOXO_POSDATE
10120   22.11.1993   05.04.1991
10261   09.02.1998   23.02.1996

以下是上述查询中的前两条记录:

CMVSC.ID CMV_NEGDATE CMV_POSDATE TOXOSC.ID TOXO_NEGDATE TOXO_POSDATE
10120    22.11.1993  05.04.1991         
10245    04.02.2010  28.06.1999         

请注意 TOXOSC< 中的记录如何/code> 被分配给 CMV* 变量,就像来自 CMVSC 的记录一样。

这怎么可能?

I attempt to do a full outer join of two tables CMVSC and TOXOSC in MS Access in order to examine which IDs are contained in both/just one of the tables:

(SELECT * FROM CMVSC LEFT JOIN TOXOSC ON CMVSC.ID = TOXOSC.ID)
UNION 
(SELECT * FROM TOXOSC LEFT JOIN CMVSC ON TOXOSC.ID = CMVSC.ID);

The query can be executed, but does not yield what I expected.

The first few records (sorted by ascending ID) in CMVSC are:

ID      CMV_NEGDATE CMV_POSDATE
10245   04.02.2010  28.06.1999
10642   08.10.1998  05.09.1991

The first few records (sorted by ascending ID) in TOXOSC are:

ID      TOXO_NEGDATE TOXO_POSDATE
10120   22.11.1993   05.04.1991
10261   09.02.1998   23.02.1996

And here are the first two records from the above query:

CMVSC.ID CMV_NEGDATE CMV_POSDATE TOXOSC.ID TOXO_NEGDATE TOXO_POSDATE
10120    22.11.1993  05.04.1991         
10245    04.02.2010  28.06.1999         

Note how the record from TOXOSC got assigned to the CMV* variables, just as the record from CMVSC.

How is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-12-07 22:30:04

原因是第一个子查询首先有 CMVSC 表,然后是 TOXOSC 表。第二个子查询的顺序相反。 UNION 获取列,并使用第一个子查询的标题(因此混合)作为列标题。试试这个:

( SELECT CMVSC.*, TOXOSC.* FROM CMVSC LEFT JOIN TOXOSC ON CMVSC.ID = TOXOSC.ID)
UNION 
( SELECT CMVSC.*, TOXOSC.* FROM TOXOSC LEFT JOIN CMVSC ON TOXOSC.ID = CMVSC.ID);

另一种方法是同时使用 LEFTRIGHT 连接:

(SELECT * FROM CMVSC LEFT JOIN TOXOSC ON CMVSC.ID = TOXOSC.ID)
UNION 
(SELECT * FROM CMVSC RIGHT JOIN TOXOSC ON TOXOSC.ID = CMVSC.ID);

The reason is that first subquery has first the CMVSC table and then the TOXOSC one. The second subquery has them in opposite order. The UNION takes the columns as they come and uses as columns titles the titles from the first subquery (therefore the mixing). Try this:

( SELECT CMVSC.*, TOXOSC.* FROM CMVSC LEFT JOIN TOXOSC ON CMVSC.ID = TOXOSC.ID)
UNION 
( SELECT CMVSC.*, TOXOSC.* FROM TOXOSC LEFT JOIN CMVSC ON TOXOSC.ID = CMVSC.ID);

Another way would be to use both LEFT and RIGHT join:

(SELECT * FROM CMVSC LEFT JOIN TOXOSC ON CMVSC.ID = TOXOSC.ID)
UNION 
(SELECT * FROM CMVSC RIGHT JOIN TOXOSC ON TOXOSC.ID = CMVSC.ID);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文