如何并排比较两个mysql表

如何并排比较两个mysql表,mysql,join,comparison,full-outer-join,Mysql,Join,Comparison,Full Outer Join,我有两个结构相同的mysql表。我需要做的是比较两个表的内容。关于如何显示一个表中的行而不是另一个表中的行,有很多答案,但我需要的是一点不同。我需要输出一个表,其中每一行包含两个表的列。如果在一个表中找不到匹配项,则列需要包含NULL 虽然id是主键,但这两个表之间的id是不同的。例如,假设我有以下两个表 表1 +----+---------+------------+---------+-----------+ | id | alias | short_name | country | r

我有两个结构相同的mysql表。我需要做的是比较两个表的内容。关于如何显示一个表中的行而不是另一个表中的行,有很多答案,但我需要的是一点不同。我需要输出一个表,其中每一行包含两个表的列。如果在一个表中找不到匹配项,则列需要包含NULL

虽然id是主键,但这两个表之间的id是不同的。例如,假设我有以下两个表

表1

+----+---------+------------+---------+-----------+
| id | alias   | short_name | country | role      |
+----+---------+------------+---------+-----------+
| 1  | alias_1 | Product 1  | USA     | retail    |
+----+---------+------------+---------+-----------+
| 2  | alias_1 | Product 1  | USA     | corporate |
+----+---------+------------+---------+-----------+
| 3  | alias_1 | Product 1  | POL     | retail    |
+----+---------+------------+---------+-----------+
| 4  | alias_1 | Product 1  | BEL     | corporate |
+----+---------+------------+---------+-----------+
| 5  | alias_2 | Product 2  | USA     | retail    |
+----+---------+------------+---------+-----------+
| 6  | alias_2 | Product 2  | BEL     | corporate |
+----+---------+------------+---------+-----------+
| 7  | alias_2 | Product 2  | BEL     | retail    |
+----+---------+------------+---------+-----------+
表2

+----+---------+------------+---------+-----------+
| id | alias   | short_name | country | role      |
+----+---------+------------+---------+-----------+
| 10 | alias_1 | Product 1  | USA     | retail    |
+----+---------+------------+---------+-----------+
| 13 | alias_1 | Product 1  | USA     | corporate |
+----+---------+------------+---------+-----------+
| 14 | alias_1 | Product 1  | POL     | corporate |
+----+---------+------------+---------+-----------+
| 16 | alias_1 | Product 1  | BEL     | retail    |
+----+---------+------------+---------+-----------+
| 17 | alias_2 | Product 2  | USA     | retail    |
+----+---------+------------+---------+-----------+
| 22 | alias_2 | Product 2  | BEL     | corporate |
+----+---------+------------+---------+-----------+
| 25 | alias_2 | Product 2  | BEL     | retail    |
+----+---------+------------+---------+-----------+
| 22 | alias_3 | Product 3  | BEL     | corporate |
+----+---------+------------+---------+-----------+
| 25 | alias_3 | Product 3  | BEL     | retail    |
+----+---------+------------+---------+-----------+
我期望的结果是:

+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| t1_alias | t1_short_name | t1_country | t1_role   | t2_alias | t2_short_name | t2_country | t2_role   |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_1  | Product 1     | USA        | retail    | alias_1  | Product 1     | USA        | retail    |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_1  | Product 1     | USA        | corporate | alias_1  | Product 1     | USA        | corporate |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_1  | Product 1     | POL        | retail    | <NULL>   | <NULL>        | <NULL>     |  <NULL>   |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_1  | Product 1     | BEL        | corporate | <NULL>   | <NULL>        | <NULL>     |  <NULL>   |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_2  | Product 2     | USA        | retail    | alias_2  | Product 2     | USA        | retail    |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_2  | Product 2     | BEL        | corporate | alias_2  | Product 2     | BEL        | corporate |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| alias_2  | Product 2     | BEL        | retail    | alias_2  | Product 2     | BEL        | retail    |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| <NULL>   | <NULL>        | <NULL>     |  <NULL>   | alias_1  | Product 1     | POL        | corporate |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| <NULL>   | <NULL>        | <NULL>     |  <NULL>   | alias_1  | Product 1     | BEL        | retail    |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| <NULL>   | <NULL>        | <NULL>     |  <NULL>   | alias_3  | Product 3     | BEL        | corporate |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
| <NULL>   | <NULL>        | <NULL>     |  <NULL>   | alias_3  | Product 3     | BEL        | retail    |
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|t1|U别名| t1|U简称| t1|U国家| t1|U角色| t2|U别名| t2|U简称| t2|U国家| t2|U角色|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名1 |产品1 |美国|零售|别名1 |产品1 |美国|零售|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名1 |产品1 |美国|企业|别名1 |产品1 |美国|企业|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名|产品1 |油料|零售| | | | ||
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名|产品1 |贝尔|公司| | | | ||
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名2 |产品2 |美国|零售|别名2 |产品2 |美国|零售|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名2 |产品2 |贝尔|公司|别名2 |产品2 |贝尔|公司|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|别名2 |产品2 |贝尔|零售|别名2 |产品2 |贝尔|零售|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|| | | | | |别名|产品1 | POL |公司|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|| | | | |别名| 1 |产品1 |贝尔|零售|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|| | | | |别名| 3 |产品3 |贝尔|公司|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
|| | | | |别名| 3 |产品3 |贝尔|零售|
+----------+---------------+------------+-----------+----------+---------------+------------+-----------+
这可能吗?我尝试了很多次,最近一次是在这里


谢谢

您想要的结果是模拟的
完全外部
连接。
实现该解决方案的方法是通过
联合
模拟
完全外部
连接,并在
右侧
连接的
WHERE
子句中应用一个条件:

SELECT 
  table_1.alias t1_alias, table_1.short_name t1_short_name, 
  table_1.country t1_country, table_1.role t1_role,
  table_2.alias t2_alias, table_2.short_name t2_short_name, 
  table_2.country t2_country, table_2.role t2_role
FROM table_1
LEFT JOIN table_2 
ON table_1.alias = table_2.alias AND table_1.short_name = table_2.short_name 
AND table_1.country = table_2.country
AND table_1.role = table_2.role
UNION ALL
SELECT 
  table_1.alias t1_alias, table_1.short_name t1_short_name, 
  table_1.country t1_country, table_1.role t1_role,
  table_2.alias t2_alias, table_2.short_name t2_short_name, 
  table_2.country t2_country, table_2.role t2_role
FROM table_1
RIGHT JOIN table_2 
ON table_1.alias = table_2.alias AND table_1.short_name = table_2.short_name 
AND table_1.country = table_2.country
AND table_1.role = table_2.role
WHERE table_1.alias IS NULL
ORDER BY t1_alias IS NULL, t1_alias
请参阅。
结果:


您想要的结果是模拟的
完全外部连接。
实现该解决方案的方法是通过
联合
模拟
完全外部
连接,并在
右侧
连接的
WHERE
子句中应用一个条件:

SELECT 
  table_1.alias t1_alias, table_1.short_name t1_short_name, 
  table_1.country t1_country, table_1.role t1_role,
  table_2.alias t2_alias, table_2.short_name t2_short_name, 
  table_2.country t2_country, table_2.role t2_role
FROM table_1
LEFT JOIN table_2 
ON table_1.alias = table_2.alias AND table_1.short_name = table_2.short_name 
AND table_1.country = table_2.country
AND table_1.role = table_2.role
UNION ALL
SELECT 
  table_1.alias t1_alias, table_1.short_name t1_short_name, 
  table_1.country t1_country, table_1.role t1_role,
  table_2.alias t2_alias, table_2.short_name t2_short_name, 
  table_2.country t2_country, table_2.role t2_role
FROM table_1
RIGHT JOIN table_2 
ON table_1.alias = table_2.alias AND table_1.short_name = table_2.short_name 
AND table_1.country = table_2.country
AND table_1.role = table_2.role
WHERE table_1.alias IS NULL
ORDER BY t1_alias IS NULL, t1_alias
请参阅。
结果:


您希望在任何条件下都有一个完整的外部联接,而不引入空值。如何在MySQL中做到这一点是一个常见问题。PS请把你的问题代码作为文本放在你的帖子里。你想要一个完整的外部连接,不管标准给出了什么行,没有引入空值。如何在MySQL中做到这一点是一个常见问题。请把你的问题代码作为文本放在你的帖子里。这正是我需要的。谢谢你的帮助这正是我需要的。谢谢你的帮助