[英]Having issues using a Right Join in MS Access 2010 with two tables that have the same fields
I have two tables, Table A and Table B. Each table have 4 fields, the name of the fields are the same for both. 我有两个表,表A和表B。每个表都有4个字段,两个字段的名称相同。 Both tables are extracted from other tables, and each record acts as a primary key.
这两个表都是从其他表中提取的,每个记录都充当主键。
I want to write a query in MS Access 2010 that gets the data unique to Table B and not shared with Table A. I am using the following image as a reference, and it looks like I need to do a Right Join. 我想在MS Access 2010中编写一个查询,该查询获取表B唯一的数据,并且不与表A共享。我使用以下图像作为参考,看起来我需要进行右连接。
Hello. 你好。 There is something not right with my SQL, I've tested it and I am getting the incorrect result.
我的SQL有一些不正确的地方,我已经对其进行了测试,但得到的结果不正确。 Below is the closest I've gotten:
以下是我最近得到的:
SELECT DISTINCT TableB.*
FROM TableB RIGHT JOIN TableA ON (TableB.Field1 = TableA.Field1) AND (TableB.Field2 = TableA.Field2) AND (TableB.Field3 = TableA.Field3) AND (TableB.Field4 = TableA.Field4)
WHERE (((TableA.Field1) Is Null));
I think it would be clearer for you to use not exists
: 我认为使用
not exists
会更清楚:
select tableb.*
from tableb
where not exists (select 1
from tablea
where (TableB.Field1 = TableA.Field1) AND (TableB.Field2 = TableA.Field2) AND (TableB.Field3 = TableA.Field3) AND (TableB.Field4 = TableA.Field4)
);
Your use of RIGHT JOIN
is incorrect. 您对
RIGHT JOIN
使用不正确。 As phrased, you want a LEFT JOIN
. 如您所说,您想要一个
LEFT JOIN
。 That is, you want to keep all rows in the first table (the "left" table in the JOIN
) regardless of whether or not a match exists in the second table. 也就是说,无论第二个表中是否存在匹配项,您都希望将所有行保留在第一个表中(
JOIN
的“左”表)。 However, the NOT EXISTS
does the same thing and the logic is a bit clearer. 但是,
NOT EXISTS
做同样的事情,逻辑上会更清楚一些。
You want to have right join if tablea is in your select statement, but as you have 如果tablea在您的select语句中,则您希望拥有正确的联接,但是
SELECT DISTINCT TableB.*
you may want to have a left join instead. 您可能需要左连接。 My suggestion would be changing your code from right to left join.
我的建议是将您的代码从右连接更改为左连接。 TableB acts like table A from venn diagrams above.
TableB的作用类似于上面的维恩图中的表A。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.