在我的数据库中创建视图时需要帮助 sql
Need assistance with creating view in my database sql
我正在尝试根据我的架构构建视图。但是现在我完全迷失了从哪里开始。我的视图应该包含有关玩家的(LINKING)监护人、监护人的 phone 号码以及他们目前正在玩的球队的信息。如果有人能帮助我走上正确的道路,我将不胜感激。请参阅 link 以获取附加图片
谢谢!
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 |
+----------+--------------+
更新:
guardian table 用于标识玩家的监护人。我们尝试使用家庭 table 到 link 玩家及其监护人,因为这种关系是多对多的。但是,我们正在努力尝试编写查询。我在想这是三个 table 加入并使用别名?但是我无法让它工作。
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 中,供那些想用我们拥有的数据帮助测试查询的人使用。
规范化您的模式会大大简化事情,但这最好作为一个单独的问题来处理。
创建您需要的复杂联接的最佳方法是通过简单联接构建它,在每个步骤中进行测试以确保您走在正确的轨道上。在这种情况下,对于没有经验的开发人员来说,唯一真正的绊脚石是需要加入 person
两次;这可以通过使用 table 别名来解决。
让我们分解一下。
当然第一步是加入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
的原因稍后会变得清晰。
接下来,我们对 player
和 person
做同样的事情来获取球员的名字:
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
table 加入前面两个步骤的结果,像这样:
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
。这给出了对 table 的第二个引用,允许结果包含来自两个引用中不同行的数据。
获取 phone 数字是一个简单的一步连接到 phone
。
获取团队名称需要分两步连接到 team_allocation
,然后连接到 team
。您只是不 return 来自 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;
希望对您有所帮助。
我正在尝试根据我的架构构建视图。但是现在我完全迷失了从哪里开始。我的视图应该包含有关玩家的(LINKING)监护人、监护人的 phone 号码以及他们目前正在玩的球队的信息。如果有人能帮助我走上正确的道路,我将不胜感激。请参阅 link 以获取附加图片
谢谢!
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 |
+----------+--------------+
更新: guardian table 用于标识玩家的监护人。我们尝试使用家庭 table 到 link 玩家及其监护人,因为这种关系是多对多的。但是,我们正在努力尝试编写查询。我在想这是三个 table 加入并使用别名?但是我无法让它工作。
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 中,供那些想用我们拥有的数据帮助测试查询的人使用。
规范化您的模式会大大简化事情,但这最好作为一个单独的问题来处理。
创建您需要的复杂联接的最佳方法是通过简单联接构建它,在每个步骤中进行测试以确保您走在正确的轨道上。在这种情况下,对于没有经验的开发人员来说,唯一真正的绊脚石是需要加入 person
两次;这可以通过使用 table 别名来解决。
让我们分解一下。
当然第一步是加入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
的原因稍后会变得清晰。
接下来,我们对 player
和 person
做同样的事情来获取球员的名字:
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
table 加入前面两个步骤的结果,像这样:
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
。这给出了对 table 的第二个引用,允许结果包含来自两个引用中不同行的数据。
获取 phone 数字是一个简单的一步连接到 phone
。
获取团队名称需要分两步连接到 team_allocation
,然后连接到 team
。您只是不 return 来自 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;
希望对您有所帮助。