需要帮助在我的数据库sql中创建视图

时间:2016-06-05 00:49:15

标签: mysql sql database database-view

Click img for schema

我试图根据我的架构构建视图。但是现在我完全迷失在哪里开始。我的观点应该是关于玩家的(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,适用于那些希望使用我们拥有的数据帮助测试查询的用户。

1 个答案:

答案 0 :(得分:1)

规范化架构会大大简化事情,但最好将其作为一个单独的问题处理。

创建复杂连接的最佳方式就像你需要的那样,是通过简单连接来构建它,在每一步测试以确保你处于正确的轨道上。在这种情况下,对于没有经验的开发者来说唯一真正的绊脚石是需要加入person两次;这可以通过使用表别名来解决。

让我们分解。

当然,第一步是将guardian加入person以获取监护人'名称:

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;

我将person别名为p1而不仅仅是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;

同样,我将person别名为p2,而不仅仅是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;

希望有所帮助。