我正在尝试根据我拥有的架构构建一个视图。但现在我完全迷失了从哪里开始。我的视图应该包括有关玩家他们(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,供那些希望使用我们拥有的数据帮助测试查询的人使用。