【问题标题】:How to check two different databases for same content of column in MySQL? [duplicate]如何在 MySQL 中检查两个不同的数据库中列的相同内容? [复制]
【发布时间】:2020-11-08 23:11:32
【问题描述】:

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

例如,我的 database1 包含以下数据:

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

和 database2 有这些数据:

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

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

【问题讨论】:

  • 这些看起来更像是表而不是数据库
  • 这将是 INTERSECT 运算符的工作,mysql 没有实现,但可以像这样模拟mysqltutorial.org/mysql-intersect
  • @Strawberry 是的,你是对的,我的错。

标签: mysql sql database subquery


【解决方案1】:

大概是指table而不是database

如果你想要两个表的所有列的值都相同的记录,那么你可以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 中的 names 存在于 table2 中,然后:

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

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多