簡體   English   中英

匹配來自不同表的行 - MySQL

[英]Matching rows from different tables - MySQL

我有這些包含以下列的表格:

    • sanit, sanit2
    • sube,id_pes,已批准

我想找到出現在這兩個表中的行。 因此,每列的行必須具有相同的值。 我嘗試了以下代碼。 但它不起作用。

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);

一旦我知道出現在兩個表中的所有行,我將使用這些行創建一個新表。

我想你想要一個子查詢。 在 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 );

即使某些列是 NULL,此查詢也會返回所有匹配的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM