0

单击 img 获取架构

我正在尝试根据我拥有的架构构建一个视图。但现在我完全迷失了从哪里开始。我的视图应该包括有关玩家他们(LINKING)监护人的信息、监护人的电话号码以及他们目前效力的球队。如果有人能帮助我走上正确的道路,我将不胜感激。请参阅附件图片的链接

谢谢!

Im hoping for this kind of result

 +----------+--------------+-------------+----------+-----------+-----------+------------+
|guardianID|guardian.fName|guardianlName|Contact   |playerfName|playerlName|teamName    |
+----------+--------------+-------------+----------+------------+-----------+-----------+
|        1 |Dora          | Kruger      |0288235556|Daphne     |Kruger     |Taeiri Eels |
|        2 |Tonia         | Wesley      |0204156513|Brigitta   |Wesley     |Southern Mag|
|        3 |Shanene       | Genovese    |0211915599|Stefan     |Genovese   |Kaikorai    |
|        4 |Jacob         | Jefferson   |0282316812|Yvonne     |Jefferson  |Pirate Skull|
|        5 |Tamika        | Longstaff   |0264217492|Todd       |Longstaff  |Zingari     |
+----------+--------------+------------+-----------+-----------+-----------+------------+

我有以下数据

====PERSON TABLE====
+----------+--------------+-------------+----------+-----------+
|personID  |firstName     |lastName     |addressID |photo      |
+----------+--------------+-------------+----------+-----------+
|        1 |Dora          | Kruger      |1         |NULL       |
|        2 |Daphne        | Kruger      |2         |NULL       |
|        3 |Tonia         | Wesley      |3         |NULL       |
|        4 |Brigitta      | Wesley      |4         |NULL       |
|        5 |Shanene       | Genovese    |5         |NULL       |
|        6 |Stefan        | Genovese    |6         |NULL       |
|        7 |Jacob         | Jefferson   |7         |NULL       |
|        8 |Yvibbe        | Jefferson   |8         |NULL       |
|        9 |Tamika        | Longstaff   |9         |NULL       |
|        10|Todd          | Longstaff   |10        |NULL       |
+----------+--------------+------------+-----------+-----------+    


====PHONE TABLE====
+----------+--------------+
|phoneID   |primaryContact|
+----------+--------------+
|        1 |0288235556    |
|        2 |0204156513    |
|        3 |0211915599    |
|        4 |0282316812    |
|        5 |0264217492    |
+----------+--------------+

====GUARDIAN TABLE====
+----------+--------------+-------------+----------+
|guardianID|personID      |phoneID      |email     |
+----------+--------------+-------------+----------+
|        1 |  1           | 1           |@ya.com   |
|        2 |  3           | 2           |@yy.com   |
|        3 |  5           | 3           |@ll.com   |
|        4 |  7           | 4           |@pp.com   |
|        5 |  9           | 5           |@no.com   |
+----------+--------------+------------+-----------+    

====PLAYER TABLE====
+----------+--------------+-------------+--------------+
|playerId  |personID      |schoolID     |dob           |
+----------+--------------+-------------+--------------+
|        1 |  2           | 1           |2008-04-06    |
|        2 |  4           | 2           |2011-11-19    |
|        3 |  6           | 3           |2011-01-07    |
|        4 |  8           | 4           |2006-01-01    |
|        5 |  10          | 5           |2004-04-06    |
+----------+--------------+-------------+--------------+


    ====FAMILY TABLE====
+----------+--------------+
|playerID   |guardianID    |
+----------+--------------+
|        1 |1             |
|        2 |2             |
|        3 |3             |
|        4 |4             |
|        5 |5             |
+----------+--------------+


====TEAM TABLE====
+----------+------------------+-------------+--------------+--------------+
|teamID    |teamName          |year         |minAge        |maxAge        |
+----------+------------------+-------------+--------------+--------------+
|        1 |  Taieri Eels     | 2015-01-01  |5             |6             |
|        2 |  Southern Magpies| 2014-01-01  |7             |8             |
|        3 |  Kaikorai        | 2016-01-01  |7             |10            |
|        4 |  Pirate Skulls   | 2014-01-01  |11            |12            |
|        5 |  Zingari         | 2013-01-01  |12            |16            |
+----------+------------------+-------------+--------------+--------------+


    ====TEAM ALLOCATION TABLE====  // allocating team for each player
+----------+--------------+
|teamID    |playerID      |
+----------+--------------+
|        2 |1             |
|        5 |2             |
|        1 |3             |
|        3 |4             |
|        4 |5             |
+----------+--------------+

更新:监护人表旨在识别玩家的监护人。我们试图使用家庭表将玩家与他们的监护人联系起来,因为这种关系是多对多的。但是,我们正在努力尝试编写查询。我在想这是一个三表连接并使用别名?但我无法让它工作。

SELECT p1.firstName AS guardianFName, p1.lastName AS guardianLName,
p2.firstName AS playerFName, p2.lastName AS playerLName
from person AS p1, person as p2
join guardian on
person.personID = guardian.personID join
family on guardian.guardianID = family.guardianID
join player on 
person.personID = player.personID join
family on player.playerID = family.playerID;

我已经尝试了几个这样的变体

DDL 和 DML 脚本位于http://pastebin.com/euVanc2e,供那些希望使用我们拥有的数据帮助测试查询的人使用。

4

1 回答 1

1

规范化您的模式将大大简化事情,但这最好作为一个单独的问题处理。

创建您需要的复杂联接的最佳方法是从简单联接构建它,在每个步骤中进行测试以确保您走在正确的轨道上。在这种情况下,对于没有经验的开发人员来说,唯一真正的绊脚石是需要加入person两次;这可以通过使用表别名来解决。

让我们分解一下。

当然,第一步是加入guardianperson获取监护人的名字:

SELECT g.guardianId AS guardianId,
       p1.firstName AS guardianFName, p1.lastName AS guardianLName
  FROM guardian g
  INNER JOIN person p1
    ON g.personId = p1.personId;

我使用别名personasp1而不仅仅是 as的原因p将在稍后变得清晰。

接下来,我们对playerperson获取玩家的名字做同样的事情:

SELECT pr.playerId AS playerId,
       p2.firstName AS playerFName, p2.lastName AS playerLName
  FROM player pr
  INNER JOIN person p2
    ON pr.personId = p2.personId;

再一次,我别名personp2而不是简单p的 . playerId最终查询不会显示该列;它在这里仅用于说明目的。

现在我们要将监护人的名字与玩家的名字相匹配。这意味着对于结果中的每一行,我们需要从中读取person。我们通过创建两个person使用两个不同别名的引用来做到这一点。我们只需通过family表格将前两个步骤的结果连接起来,如下所示:

SELECT g.guardianId AS guardianId,
       p1.firstName AS guardianFName, p1.lastName AS guardianLName,
       p2.firstName AS playerFName, p2.lastName AS playerLName
  FROM guardian g
  INNER JOIN person p1
    ON g.personId = p1.personId
  INNER JOIN family f
    ON g.guardianId = f.guardianId
  INNER JOIN player pr
    ON f.playerId = pr.playerId
  INNER JOIN person p2
    ON pr.personId = p2.personId;

请注意对 的第二个引用person,别名为p2。这给出了对表的第二个引用,允许结果包含来自两个引用中不同行的数据。

获取电话号码是一个简单的一步加入phone

获取团队名称需要两步加入team_allocation,然后加入team。您根本不从team_allocation.

完整的查询应如下所示:

SELECT g.guardianId AS guardianId,
       p1.firstName AS guardianFName, p1.lastName AS guardianLName,
       ph.primaryContact AS contact,
       p2.firstName AS playerFName, p2.lastName AS playerLName,
       t.teamName AS teamName
  FROM guardian g
  INNER JOIN person p1
    ON g.personId = p1.personId
  INNER JOIN family f
    ON g.guardianId = f.guardianId
  INNER JOIN player pr
    ON f.playerId = pr.playerId
  INNER JOIN person p2
    ON pr.personId = p2.personId
  INNER JOIN phone ph
    ON g.phoneId = ph.phoneId
  INNER JOIN team_allocation ta
    ON ta.playerId = pr.playerId
  INNER JOIN team t
    ON ta.teamId = t.teamId;

希望有帮助。

于 2016-06-05T02:22:28.387 回答