匹配来自不同表的行 - MySQL

Matching rows from different tables - MySQL

我有这些 table 以下列:

我想找到出现在这两个 table 中的行。因此,行必须对每一列具有相同的值。 我尝试了以下代码。但是没用。

select sube, id_pes, approved from sanit 
    where  sube in (select sube from sanit2) and 
           id_pes in (select id_pes from sanit2) and 
           approved in (select approved from sanit2);

一旦我知道出现在两个 table 中的所有行,我将使用这些行创建一个新的 table。

我想你想要一个子查询。在 MySQL 中,您可以使用 in:

select sube, id_pes, approved
from sanit 
where (sube, id_pes, approved) in (select sube, id_pes, approved from sanit2);

请注意,NULL 值将无法通过比较。

SELECT sube, id_pes, approved
FROM sanit 
WHERE EXISTS ( SELECT NULL
               FROM sanit2
               WHERE sanit.sube <=> sanit2.sube
                 AND sanit.id_pes <=> sanit2.id_pes
                 AND sanit.approved <=> sanit2.approved );

此查询将 return 所有匹配的行,即使某些列为 NULL。