简体   繁体   English

SQL如何比较两个不同表中的两列

[英]SQL how to compare two columns from two different tables

I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns.我有两个表,其中表 1 包含 4 列,而表 2 包含 8 列。 I have two columns in table1 that I want to compare them with two columns in table2.我在 table1 中有两列,我想将它们与 table2 中的两列进行比较。

Table 1 have column1 and column2 (that needs to be compared)
Table 2 have column6 and column7 (that needs to be compared) 

I need to compare the combination of the two columns.我需要比较两列的组合。 I tried to do the below query however it doesn't work我试图做下面的查询,但它不起作用

Select * from table1 
where column1, column2 NOT IN (Select column6, column7 from table2)

How can I compare the two columns in the the two tables?如何比较两个表中的两列?

Try a minus statement.尝试减号语句。 This will give you any results from the first select statement of table1 that aren't in the second select statement on table2.这将为您提供 table1 的第一个 select 语句中不在 table2 上的第二个 select 语句中的任何结果。

select column1, column2 from table1
minus
select column6, column7 from table2

Except shows the difference between two tables (the Oracle guys use minus instead of except and the syntax and use is the same).除了显示两个表之间的差异(Oracle 人员使用减号代替除外,语法和用法是相同的)。 It is used to compare the differences between two tables.它用于比较两个表之间的差异。 For example, let's see the differences between the two tables例如,让我们看看两个表之间的差异

SELECT * FROM
 table1
EXCEPT
SELECT * FROM
 table2

NOT EXISTS is a "null safe" version of NOT IN . NOT EXISTSNOT IN的“空安全”版本。 If you mean the combination column1 AND column2 not in same row in table2:如果您的意思是组合 column1 AND column2 不在 table2 的同一行中:

select *
from table1
where NOT EXISTS (select 1 from table2
                  where table1.column1 = table2.column6
                    and table1.column2 = table2.column7)

Or if you mean just column1 and column2 values can't even be in different rows in table2:或者,如果您的意思只是 column1 和 column2 值甚至不能在 table2 中的不同行中:

select *
from table1
where NOT EXISTS (select 1 from table2
                  where table1.column1 = table2.column6)
  and NOT EXISTS (select 1 from table2
                  where table1.column2 = table2.column7)

The query with the least comparisions I can think of is我能想到的比较最少的查询是

Select t1.* 
from table1 t1
left join table2 t2 on t1.column1 in (t2.column6, t2.column7)
                    or t1.column2 in (t2.column6, t2.column7)
where t2.column6 is null
    select * from table1 where column1 not in(select column 6 from table2) or column2 not in(select column7 from table2)

This will give you rows from table1 where there are differences between col1 and col6 or col2 and col7这将为您提供 table1 中的行,其中 col1 和 col6 或 col2 和 col7 之间存在差异

Hope this helps希望这可以帮助

SELECT *  FROM table1 t1
RIGHT JOIN table2 t2
WHERE
t1.c1 = t2.c6 AND
t1.c2 = t2.c7

Please try below query.请尝试以下查询。

 Select case when (table1.column1=table2.column6) then 1 else 0 end coluumn1_6 
 check , case when (table1.column2 =table2.column7 ) then 1 else 0 end

     from table1 
    inner join
     table2
on table1.ID=Table2.ID

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

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