开发者

Inexplicable behaviour in MS Access full outer join?

I attempt t开发者_运维问答o 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?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜