如何检查两个不同的数据库中 MySQL 列的相同内容?

How to check two different databases for same content of column in MySQL?

我想知道如何在两个不同的数据库中检查列中的相同内容。

例如,我有包含以下数据的数据库 1:

+----------+------------+------------------+
| actor_id | actor_user | actor_name       |
+----------+------------+------------------+
|        1 |     234287 | User1            |
|        2 |      47689 | User2            |
|        3 |     235133 | User3            |
|        4 |      62861 | User4            |
|        5 |     190486 | User5            |
+----------+------------+------------------+

数据库 2 包含此数据:

+----------+------------+------------------+
| actor_id | actor_user | actor_name       |
+----------+------------+------------------+
|        1 |     234257 | User5            |
|        2 |      47619 | User6            |
|        3 |     235123 | User7            |
|        4 |      62811 | User8            |
|        5 |     190436 | User9            |
+----------+------------+------------------+

我需要将 User5 显示为输出的查询。

据推测,您的意思是 table 而不是 database.

如果您想要在两个 table 的所有列中具有相同值的记录,那么您可以 join:

select t1.*
from table1 t1
inner join table2 t2
    on  t2.actor_id = t1.actor_id 
    and t2.actor_user = t1.actor_user 
    and t2.actor_name = t1.actor_name

如果您想要选择性匹配,exists 可能更合适 - 例如,table1 中的 name 存在于 table2 中,那么:

select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.name = t1.name)