2017-02-09 76 views
1

问题在于“不受MySQL支持”的“EXCEPT”。目标是找到所有列(id除外)不相同的所有行。如何将使用“EXCEPT”的MSSQL查询转换为MySQL?

SELECT B.*, 'modified' AS 'changetype' 
FROM (
     SELECT * FROM table1 
     EXCEPT 
     SELECT * FROM table2 
) S1 
INNER JOIN table2 B ON S1.id = B.id; 

回答

1

这很棘手。你将不得不列出MySQL中的所有列。这可能是最接近你想要的:

select t2.* 
from table2 t2 
where not exists (select 1 
        from table1 t1 
        where t1.id = t2.id and 
         t1.col1 = t2.col1 and 
         t1.col2 = t2.col2 and 
         . . . 
       ); 
+0

我是否正确,这也捕捉记录存在于一张表,但没有其他? –

+0

@Fo。 。 。 。就像你问题中的查询一样,这只会从'table2'返回id。 –