当匹配或喜欢时,如何比较2个不同表中的相同字段?

问题描述 投票:0回答:4

我需要比较2个表(字段是代码)并在名称匹配或喜欢时创建第三个(使用每个表的结果)。

table1:姓氏姓氏年龄

table2:地址城市代码

我需要一个新表,其中包含table1和table2中的代码,以查看差异。

每个字段都是文本,如a345694s3。

ms-access compare
4个回答
0
投票

我不确定,但你可以尝试在哪里条件..例如:select * from tab1 t1, tab2 t2 where 't1.code'='t2.code'

如果你得到上面代码的结果,那就把它作为一个子查询!!


0
投票

如果您正在尝试查看table1与table2中的代码,请尝试FULL OUTER JOIN

SELECT t1.code AS T1, t2.code as T2
FROM   table1 t1
FULL   OUTER JOIN table t2
       ON t1.code = t2.code

结果:

T1     T2
====   ====
A      NULL  --'A' is in table1 but not table2
NULL   B     --'B' is in table2 but not table1
C      C     --'C' is in both tables

编辑

由于FULL OUTER JOIN在MS Access中不可用,您可以使用INNER JOIN,LEFT OUTER JOIN和RIGHT OUTER JOIN来实现与讨论的here相同的内容。


0
投票

从您的评论中,您希望找到导入严重的任何不匹配的行。我首先会找到正确导入的所有匹配行,然后只选择该集合中不存在的任何内容。

SELECT * 
FROM table1 
WHERE code NOT IN (
    -- find all the matching values of code which imported correctly
    SELECT code 
    FROM table1 
      JOIN table2 ON table1.code = table2.code
)

这不是一个快速查询,但由于它是完整性检查,因此在初始导入后不需要在生产中运行,因此速度不是主要问题。


0
投票
SELECT u.iduser, a.idaccount, u.usercode_user, a.usercode_accounts
FROM users AS u, accounts AS a
WHERE (((a.usercode_accounts) Like "*" & [u].[usercode_user] & "*"));
© www.soinside.com 2019 - 2024. All rights reserved.