MYSQL 创建 "complex" 视图

MYSQL create "complex" view

所以我正在尝试为我和我的朋友制作一个网站。在那个网站上,我有一个开始“战争”的表格。当您提交表单时,它会保存到 table 中,其中包含字段“攻击者”和“受害者”。我还有另一个 table 命名帐户,因此他们可以登录该站点。另一个 table 有一个“id”列、“discord_id”和“用户名”。我怎样才能创建一个 MYSQL 视图来让所有列都像这样: 帐户:

+----+----------+----------------+------------+
| id | username |    password    | discord_id |
+----+----------+----------------+------------+
|  1 | user1    | users2password |  users1id  |
|  2 | user2    | users2password |  users2id  |
+----+----------+----------------+------------+

战争:


+--------+-------------+-----------+
| war_id | attacker_id | victim_id | 
+--------+-------------+-----------+
|      1 |           1 |      2    |
|      2 |           2 |      1    |
+--------+-------------+-----------+

我想要的结果:


+--------+-------------+-------------------+-------------------+-----------+-------------------+-----------------+
| war_id | attacker_id | attacker_discor_id| attacker_username | victim_id | victim_discord_id | victim_username |
+--------+-------------+-------------------+-------------------+-----------+-------------------+-----------------+
|      1 |           1 | users1discordi    | user1             |         2 |    user2discordid | user2           |
|      2 |           2 |  ...              |    ...            |   ...     |       ...         |   ...           |
+--------+-------------+-------------------+-------------------+-----------+-------------------+-----------------+


另外,我是个菜鸟,所以我不知道我是否提供了所有必要的信息。如果您需要其他信息,请询问,我会尽快答复。 提前致谢

wars table 开始,您可以加入 accounts table 两次,一次针对攻击者,另一次针对受害者 - 诀窍是给每个加入的 table 的不同别名,因此您可以明确地引用它们的列:

create view wars_view as
select w.war_id, 
    w.attacker_id, a1.discord_id attacker_discord_id, a1.username attacker_username,
    w.victim_id,   a2.discord_id victim_discord_id,   a2.username victim_username
from wars w
inner join accounts a1 on a1.id = w.attacker_id
inner join accounts a2 on a2.id = w.victim_id