有没有办法从三个不同的表中获取关联数据并显示它们?

时间:2019-05-03 23:22:01

标签: mysql

我有四个表,其中三个需要我的数据,还有第四个存储该数据如何关联的表。运行查询时,我得到的输出是错误的,似乎我没有正确地链接起来,但是我不确定该怎么做。如何链接我的联接以获取所需的输出?

到目前为止,我已经使用过

SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id=pc.player_id
JOIN classes c ON c.class_id=pc.class_id
JOIN specialization s ON s.spec_id=pc.spec_id

引用

CREATE TABLE players(
player_id INT UNSIGNED auto_increment PRIMARY KEY,
name VARCHAR(20)
)...;
CREATE TABLE classes(
class_id INT UNSIGNED auto_increment PRIMARY KEY,
class VARCHAR(20)
)...;
CREATE TABLE specializations(
spec_id INT UNSIGNED auto_increment PRIMARY KEY,
class_id INT UNSIGNED,
specialization VARCHAR(20)
)...;

但是我想以某种方式使用此表来显示正确链接的信息,但我不确定如何做到:

CREATE TABLE players_classes(
pc_id INT UNSIGNED auto_increment PRIMARY KEY,
FOREIGN KEY(class_id) REFERENCES classes(class_id),
FOREIGN KEY(player_id) REFERENCES players(player_id),
FOREIGN KEY(spec_id) REFERENCES specializations(spec_id)
)...;

我希望能够获得玩家的名字以及他们相关的职业和专长,实际结果是显示每个ID与1/1/1相关的值,依此类推。

编辑:players_classes中的数据如下

pc_id | class_id | player_id | spec_id |
   1          3           1         8
   2         12           2        35
   3          2           3         6
etc.

因此,预期结果是

class    |   name      |   spec
paladin    seranul      holypa
hunter     contherious  marksmanship
priest     unicorns     holypr

我反而得到

class      |        name       |    spec
warlock       Affliction         Affliction
warlock       Grireaver          Demonology
warlock       Affliction         Demonology

依此类推,列出了我的players_classes表中不存在的组合

专业化表

+---------+----------+----------------+
| spec_id | class_id | specialization |
+---------+----------+----------------+
|       1 |        1 | Affliction     |
|       2 |        1 | Destruction    |
|       3 |        1 | Demonology     |
|       4 |        2 | Shadow         |
|       5 |        2 | Discipline     |
|       6 |        2 | HolyPr         |
|       7 |        3 | Retribution    |
|       8 |        3 | HolyPa         |
|       9 |        3 | ProtectionPa   |
|      10 |        4 | ProtectionWa   |
|      11 |        4 | Arms           |
|      12 |        4 | Fury           |
|      13 |        5 | FrostMa        |
|      14 |        5 | Fire           |
|      15 |        5 | Arcane         |
|      16 |        6 | vengeance      |
|      17 |        6 | havoc          |
|      18 |        7 | guardian       |
|      19 |        7 | balance        |
|      20 |        7 | feral          |
|      21 |        7 | restorationDr  |
|      22 |        8 | elemental      |
|      23 |        8 | enhance        |
|      24 |        8 | restorationSh  |
|      25 |        9 | frostDk        |
|      26 |        9 | blood          |
|      27 |        9 | unholy         |
|      28 |       10 | outlaw         |
|      29 |       10 | assassin       |
|      30 |       10 | subtlety       |
|      31 |       11 | brewmaster     |
|      32 |       11 | windwalker     |
|      33 |       11 | mistweaver     |
|      34 |       12 | BeastMaster    |
|      35 |       12 | marksmanship   |
|      36 |       12 | Survival       |
+---------+----------+----------------+

课程表

+----------+-------------+
| class_id | class       |
+----------+-------------+
|        1 | warlock     |
|        2 | priest      |
|        3 | paladin     |
|        4 | warrior     |
|        5 | mage        |
|        6 | demonhunter |
|        7 | druid       |
|        8 | shaman      |
|        9 | deathknight |
|       10 | rogue       |
|       11 | monk        |
|       12 | hunter      |
+----------+-------------+

玩家表

+-----------+--------------+
| player_id | name         |
+-----------+--------------+
|         1 | Seranul      |
|         2 | Contherious  |
|         3 | Unicorns     |
|         4 | Remereili    |
|         5 | Affliction   |
|         6 | Meowing      |
|         7 | Brobot       |
|         8 | Bagelsbbq    |
|         9 | Rafusen      |
|        10 | Taiboku      |
|        11 | Yikes        |
|        12 | Thunderblaze |
|        13 | Muo          |
|        14 | Intz         |
|        15 | Trunks       |
|        16 | Kalphyte     |
|        17 | Eyeoftheshoe |
|        18 | Amuhnet      |
|        19 | Synkka       |
|        20 | Affliction   |
|        21 | Kts          |
|        22 | Shadowdreams |
|        23 | Zahel        |
|        24 | Azrama       |
|        25 | Seranul      |
|        26 | Momspaghetti |
|        27 | Ohki         |
|        28 | Rafusen      |
|        29 | Cindyy       |
|        30 | Grireaver    |
|        31 | Intz         |
|        32 | lazy         |
|        33 | missworld    |
|        34 | Affliction   |
|        35 | Amuhnet      |
|        36 | eyeoftheshoe |
|        37 | sanctus      |
|        38 | nozshelen    |
|        39 | Contherious  |
|        40 | messer       |
|        41 | catathor     |
|        42 | demonblaze   |
|        43 | wrillett     |
|        44 | raagnnar     |
|        45 | xizi         |
|        46 | nemesix      |
|        47 | zeroskill    |
|        48 | chikfillidan |
|        49 | tentenlol    |
|        50 | unicorns     |
|        51 | bubuhtide    |
|        52 | ohki         |
|        53 | azrama       |
+-----------+--------------+

players_classes表

+-------+----------+-----------+---------+
| pc_id | class_id | player_id | spec_id |
+-------+----------+-----------+---------+
|     1 |        3 |         1 |       8 |
|     2 |       12 |         2 |      35 |
|     3 |        2 |         3 |       6 |
|     4 |       11 |         4 |      31 |
|     5 |        1 |         5 |       1 |
|     6 |       12 |         6 |      34 |
|     7 |        2 |         7 |       6 |
|     8 |       11 |         8 |      31 |
|     9 |        2 |         9 |       6 |
|    10 |        7 |        10 |      21 |
|    11 |        4 |        11 |      11 |
|    12 |        8 |        12 |      22 |
|    13 |        4 |        13 |      12 |
|    14 |        5 |        14 |      13 |
|    15 |        3 |        15 |       7 |
|    16 |       11 |        16 |      33 |
|    17 |        8 |        17 |      22 |
|    18 |        2 |        18 |       6 |
|    19 |        8 |        19 |      23 |
|    20 |       11 |        20 |      33 |
|    21 |        5 |        21 |      13 |
|    22 |        6 |        22 |      17 |
|    23 |       10 |        23 |      29 |
|    24 |        8 |        24 |      22 |
|    25 |       11 |        25 |      31 |
|    26 |       11 |        26 |      32 |
|    27 |        4 |        27 |      11 |
|    28 |        5 |        28 |      13 |
|    29 |        7 |        29 |      19 |
|    30 |        1 |        30 |       3 |
|    31 |        9 |        31 |      25 |
|    32 |        3 |        32 |       8 |
|    33 |        9 |        33 |      25 |
|    34 |        1 |        34 |       3 |
|    35 |        2 |        35 |       6 |
|    36 |        4 |        36 |      11 |
|    37 |        5 |        37 |      13 |
|    38 |        5 |        38 |      13 |
|    39 |        9 |        39 |      25 |
|    40 |        6 |        40 |      17 |
|    41 |       10 |        41 |      28 |
|    42 |        2 |        42 |       6 |
|    43 |        8 |        43 |      24 |
+-------+----------+-----------+---------+

1 个答案:

答案 0 :(得分:1)

您需要在查询中使用关系表:

SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id = pc.player_id
JOIN classes c ON c.class_id = pc.class_id
JOIN specializations s ON s.spec_id = pc.spec_id

DEMO

在其他表中甚至不应该包含class_idspec_id,因为它们是多对多关系,并且这些列只能用于一对一关系。