Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 有没有办法从三个不同的表中获取相关数据并显示它们?_Mysql - Fatal编程技术网

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

Mysql 有没有办法从三个不同的表中获取相关数据并显示它们?,mysql,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.cla

我有四个表,其中三个表需要数据,第四个表存储这些数据的关联方式。当运行我的查询时,我得到了错误的输出,似乎我没有正确地连接东西,但我不确定如何这样做。我如何链接我的连接以获得我想要的输出

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

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_类的数据如下

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\u classes表中不存在的组合

+----------+-------------+
| 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      |
+----------+-------------+
+-------+----------+-----------+---------+
| 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 |
+-------+----------+-----------+---------+
专业表

+---------+----------+----------------+
| 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      |
+----------+-------------+
+-------+----------+-----------+---------+
| 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 |
+-------+----------+-----------+---------+
球员表

+-----------+--------------+
| 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       |
+-----------+--------------+
球员等级表

+----------+-------------+
| 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      |
+----------+-------------+
+-------+----------+-----------+---------+
| 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 |
+-------+----------+-----------+---------+

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

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


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

这会完全选择表中的名称数量,虽然与简单的select*from players_classes相比,它没有选择正确的信息,但它从表中选择了正确数量的名称,尽管有重复的名称包含不正确的信息。
players_classes
的每一行都应该代表一个有效的球员组合,阶级和专业化。当然你会得到重复的,因为这是一个多对多的关系。名称将出现在每个组合中。如果您获得的信息不正确,那一定是因为
players\u classes
表中的行不正确。请在问题中添加示例数据和预期结果。我怀疑您实际上需要两个不同的关系表:
players\u classes
classes\u specializations