1

我需要创建一个名为 phonelist 的视图,其中列出了每个球员、他们的监护人、监护人的电话号码和他们所在的球队。我将如何编码。您应该在下面找到您需要的所有信息:

在此处输入图像描述

这些是我的表:

CREATE TABLE Person
(
personID INT NOT NULL,
name VARCHAR(50),
address VARCHAR(70),
phone VARCHAR(15),
email VARCHAR(30),
year INT,
PRIMARY KEY (personID)
);

CREATE TABLE Player
(
personID INT NOT NULL,
dateOfBirth DATE,
school VARCHAR(30),
PRIMARY KEY (personID),
FOREIGN KEY (personID) REFERENCES Person (personID)
);

CREATE TABLE Team
(
teamID INT NOT NULL,
tName VARCHAR(70),
ageRange VARCHAR(30),
PRIMARY KEY (teamID)
);

CREATE TABLE Coach
(
personID INT NOT NULL,
dateBeganCoaching DATE,
PRIMARY KEY (personID),
FOREIGN KEY (personID) REFERENCES Person (personID)
);

CREATE TABLE coachTeam
(
personID INT NOT NULL, 
teamID INT NOT NULL,
PRIMARY KEY (personID, teamID),
FOREIGN KEY (personID) REFERENCES Coach (personID),
FOREIGN KEY (teamID) REFERENCES Team (teamID)
);

CREATE TABLE playerTeam
(
personID INT NOT NULL, 
teamID INT NOT NULL,
PRIMARY KEY (personID, teamID),
FOREIGN KEY (personID) REFERENCES Player (personID),
FOREIGN KEY (teamID) REFERENCES Team (teamID)
);

CREATE TABLE Guardian
(
parentID INT NOT NULL,
childID INT NOT NULL,
PRIMARY KEY (parentID, childID),
FOREIGN KEY (parentID) REFERENCES Person (personID),
FOREIGN KEY (childID) REFERENCES Person (personID)
);

CREATE TABLE Qualification
(
qualificationID INT NOT NULL,
qName VARCHAR(30),
PRIMARY KEY (qualificationID)
);

CREATE TABLE coachQualification
(
qualificationID INT NOT NULL,
personID INT NOT NULL,
PRIMARY KEY (personID, qualificationID),
FOREIGN KEY (qualificationID) REFERENCES Qualification (qualificationID),
FOREIGN KEY (personID) REFERENCES Coach (personID)
);

我将如何做到这一点,以便我仅使用 person 表来引用玩家和监护人,以及我将如何做到这一点,以便如果玩家有多个监护人,两个监护人将被放置在同一行而不是创建输出中的一个全新行显示两位监护人的感谢。

我尝试了以下方法,但出现了错误:

CREATE VIEW phonelist AS 
SELECT Child.name, tName, Parent.name, Parent.phone
FROM playerTeam INNER JOIN Person AS Child ON playerTeam.personID = Child.personID
                INNER JOIN Team ON playerTeam.teamID = Team.teamID
                INNER JOIN Person AS Parent ON Guardian.parentID = Parent.personID;

Select * FROM phonelist ORDER BY name ASC;
4

1 回答 1

1
Select P.ID, P.Name, P2.PersonID, P2.Name, P2.Phone, T2.tName as GuardianTeam T.tName as PersonTeam
INNER JOIN Player PL on PL.PErsonID=P.PersonID
INNER JOIN Guardian G on G.ParentID = P.PersonID
LEFT JOIN Person P2 on P2.PersonID = G.ChildID
INNER JOIN PlayerTeam PT on PT.PersonID = PL.PersonID
LEFT JOIN Player PL2 on PL2.PersonID = P2.PersonID
LEFT JOIN PlayerTeam PT2 on PT2.PersonID = PT2.PersonID
LEFT JOIN Team T2 on T2.TeamID = PT.TeamID
INNER JOIN Team T on T.TeamID = PT.TeamID

啊好的编辑删除人员团队...

归还所有身为球员的人、他们的监护人姓名、电话和监护球队以及任何没有监护人的球员;或监护人不属于球队或球员本人的情况。

Select P.ID, P.Name, P2.PersonID, P2.Name, P2.Phone, T2.tName as GuardianTeam
INNER JOIN Player PL on PL.PErsonID=P.PersonID
INNER JOIN Guardian G on G.ParentID = P.PersonID
LEFT JOIN Person P2 on P2.PersonID = G.ChildID
LEFT JOIN Player PL2 on PL2.PersonID = P2.PersonID
LEFT JOIN PlayerTeam PT2 on PT2.PersonID = PT2.PersonID
LEFT JOIN Team T2 on T2.TeamID = PT.TeamID
于 2012-05-25T00:29:45.287 回答