Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL如何比较两个不同表中的两列_Mysql_Sql_Sql Server - Fatal编程技术网

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

Mysql SQL如何比较两个不同表中的两列,mysql,sql,sql-server,Mysql,Sql,Sql Server,我有两个表,其中表1包含4列,而表2包含8列。我想将表1中的两列与表2中的两列进行比较 Table 1 have column1 and column2 (that needs to be compared) Table 2 have column6 and column7 (that needs to be compared) 我需要比较两列的组合。我试图做下面的查询,但它不工作 Select * from table1 where column1, column2 NOT IN (Se

我有两个表,其中表1包含4列,而表2包含8列。我想将表1中的两列与表2中的两列进行比较

Table 1 have column1 and column2 (that needs to be compared)
Table 2 have column6 and column7 (that needs to be compared) 
我需要比较两列的组合。我试图做下面的查询,但它不工作

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

如何比较两个表中的两列?

我能想到的比较最少的查询是

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

尝试一个减号语句。这将为您提供表1的第一条select语句中未包含在表2的第二条select语句中的任何结果

select column1, column2 from table1
minus
select column6, column7 from table2
这将给出表1中col1和col6或col2和col7之间存在差异的行


希望这有帮助

不存在
不存在的“空安全”版本。
如果您指的是表2中不在同一行的列1和列2的组合:

select *
from table1
where NOT EXISTS (select 1 from table2
                  where table1.column1 = table2.column6
                    and table1.column2 = table2.column7)
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)
或者,如果您的意思是column1和column2值甚至不能在表2中的不同行中:

select *
from table1
where NOT EXISTS (select 1 from table2
                  where table1.column1 = table2.column6
                    and table1.column2 = table2.column7)
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)

Except显示了两个表之间的差异(Oracle人员使用减号代替Except,语法和用法相同)。它用于比较两个表之间的差异。例如,让我们看看这两个表之间的差异

SELECT * FROM
 table1
EXCEPT
SELECT * FROM
 table2

是否要从t1中选择所有行,其中t1.column1 t2.column6和t1.column2 t2.column7?是否使用SQL Server和/或MySQL?(不要标记未使用的不同dbms产品!)给出一些示例数据来演示您需要的行为,我可以用几种不同的方式解释您的帖子。虽然这可能会回答问题,但您可能不想提供一些额外的细节并解释答案。在目前的情况下,这个答案是低质量的。我用这个方法取得了巨大的成功。但是,我希望有一种方法可以突出显示数据不同的确切值和列?谢谢