简体   繁体   English

比较两个不同表中的两列

[英]Comparing two columns in two different tables

I have two tables A and B where table A has a column X , table B has column Y . 我有两个表AB ,其中表A有一列X ,表B有列Y These columns contain account number information. 这些列包含帐号信息。 I want to check whether the account number information in column AX is present in BY . 我想检查BY列中是否存在列AX的帐号信息。

Note: Both column X and Y can have duplicates because these are like composite primary keys. 注意:列XY都可以有重复项,因为它们类似于复合主键。

How can I do solve this? 我怎么能解决这个问题?

This will give you account information in table A that is also present in table B . 这将为您提供表A中的帐户信息,该信息也存在于表B The check is done by a correlated subquery that checks for each row in A whether the account information is present in B . 检查由相关子查询完成,该子查询检查A每一行是否存在B的帐户信息。

SELECT DISTINCT
    X
FROM
    A
WHERE
    EXISTS (
        SELECT
            *
        FROM
            B
        WHERE
            B.Y=A.X
    );

You can use an INNER JOIN , like this: 您可以使用INNER JOIN ,如下所示:

 SELECT * 
 FROM table1 a 
 INNER JOIN table2 b 
 ON a.X = b.Y

OR 要么

you can go for IF EXISTS ,like this: 你可以去IF EXISTS ,像这样:

SELECT *
FROM table1 a
WHERE EXISTS(
              SELECT 1
              FROM table2 b
              WHERE a.x=b.Y )

select distinct(X) 选择不同(X)
from A,B 来自A,B
WHERE AX=BY WHERE AX = BY

This will give you a list of account numbers in A that are not in B: 这将为您提供A中不在B中的帐号列表:

SELECT X FROM (
    SELECT DISTINCT X FROM A
) A
LEFT JOIN B ON Y = X
WHERE Y IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM