如何在没有主键的情况下从 mySQL 中的两个表中获取不常见的记录

How to fetch uncommon records from two tables in mySQL without primary key

我有两个 table,它们都有相同数量的列,其中 none 具有如下任意主键:

Table一个

Name Ranking Genre Category Rated
M1 1 Comedy Movie G
M2 2 Action Series PG

Table B

Name Ranking Genre Category Rated
MX1 44 Thriller Series G
M2 2 Action Series PG

我需要从两个 table 中获取不常见的记录,其中 Ranking 和 Name 的组合对它们来说是唯一的。

我试过使用左连接和右连接,但它给了我其中一个 table 的所有记录。我在这里的首要任务是只获取不常见的记录,如下所示:

Name Ranking Genre Category Rated
M1 1 Comedy Movie G
MX1 44 Thriller Series G

您可以合并两个外部联接。例如:

select a.*
from a
left join b on a.name = b.name and a.ranking = b.ranking
where b.name is null
union all
select b.*
from b
left join a on a.name = b.name and a.ranking = b.ranking
where a.name is null

尝试使用内部联接

SELECT *
FROM table_a
INNER JOIN table_b ON table_a.name = table_b.name AND table_a.ranking = table_b.ranking;