2012-05-08 19 views
5

我已經成功在我的MySQL數據庫中創建一些數據的視圖,如下圖所示:如何在MySQL中設計一個「視圖」來獲得我期待的結果?

mysql> CREATE VIEW phonelist AS 
    -> SELECT parent.parentID AS ParentID, 
    -> ParentPerson.firstName AS ParentFirstName, 
    -> ParentPerson.lastName AS ParentLastName, 
    -> ChildPerson.firstName AS PlayerFirstName, 
    -> ChildPerson.lastName AS PlayerLastName, 
    -> GuardianHomePhone.homeNumber AS GuardianHomePhone 
    -> FROM parent 
    -> JOIN player ON (parent.parentID IN (player.motherID, player.fatherID)) 
    -> JOIN person AS ParentPerson ON (ParentPerson.personID = parent.parentID) 
    -> JOIN person AS ChildPerson ON (ChildPerson.personID = player.playerID) 
    -> JOIN addressDetails AS GuardianHomePhone ON (GuardianHomePhone.personID = parent.parentID); 

Query OK, 0 rows affected (0.00 sec) 

mysql> select * from phonelist; 

+----------+-----------------+----------------+-----------------+----------------+-------------------+ 
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | PlayerLastName | GuardianHomePhone | 
+----------+-----------------+----------------+-----------------+----------------+-------------------+ 
|  8 | Gregory   | Peck   | Michael   | Peck   | 034871234   | 
|  9 | Laura   | Peck   | Michael   | Peck   | 034871234   | 
|  10 | Martha   | Petersen  | Matt   | Petersen  | 034724321   | 
|  10 | Martha   | Petersen  | Christopher  | Petersen  | 034724321   | 
|  11 | Chris   | Michaels  | Richard   | Michaels  | 034791212   | 
|  11 | Chris   | Michaels  | Shaun   | Michaels  | 034791212   | 
|  12 | Nadine   | Michaels  | Richard   | Michaels  | 034791212   | 
|  12 | Nadine   | Michaels  | Shaun   | Michaels  | 034791212   | 
|  13 | Barry   | Dackers  | Harry   | Dackers  | 034871996   | 
|  14 | Kevin   | Mitchell  | Daniel   | Mitchell  | 034742886   | 
|  15 | Rebecca   | Mitchell  | Daniel   | Mitchell  | 034742886   | 
+----------+-----------------+----------------+-----------------+----------------+-------------------+ 
11 rows in set (0.00 sec) 

mysql> 

創建這個觀點是一個挑戰,但下面的說明是我無法瀏覽周圍什麼:

我還需要添加每個球員在這個視圖中的球隊。因爲讓團隊的名字匹配需要將4個表連在一起,所以我無法將事物合併在一起。我設法建立一個滿足玩家下方隊一個單獨的查詢:

mysql> select person.firstName, person.lastName, team.teamName 
    -> from person join player on person.personID = player.playerID 
    -> join teamAllocation on person.personID = teamAllocation.playerID 
    -> join team on teamAllocation.teamID = team.teamID; 

+-------------+----------+------------+ 
| firstName | lastName | teamName | 
+-------------+----------+------------+ 
| Michael  | Peck  | U10 Red | 
| Christopher | Petersen | U10 Red | 
| Richard  | Michaels | U11 Orange | 
| Shaun  | Michaels | U9 Yellow | 
| Matt  | Petersen | U11 Orange | 
| Harry  | Dackers | U9 Yellow | 
| Daniel  | Mitchell | U9 Yellow | 
+-------------+----------+------------+ 
7 rows in set (0.00 sec) 

mysql> 

我在我的腦海可視化的結果是這樣的:

+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+ 
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | TeamName  | PlayerLastName | GuardianHomePhone | 
+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+ 
|  8 | Gregory   | Peck   | Michael   | U10 Red  | Peck   | 034871234   | 
|  9 | Laura   | Peck   | Michael   | U10 Red  | Peck   | 034871234   | 
|  10 | Martha   | Petersen  | Matt   | U11 Orange  | Petersen  | 034724321   | 
|  10 | Martha   | Petersen  | Christopher  | U10 Red  | Petersen  | 034724321   | 
|  11 | Chris   | Michaels  | Richard   | U11 Orange  | Michaels  | 034791212   | 
|  11 | Chris   | Michaels  | Shaun   | U9 Yellow  | Michaels  | 034791212   | 
|  12 | Nadine   | Michaels  | Richard   | U11 Orange  | Michaels  | 034791212   | 
|  12 | Nadine   | Michaels  | Shaun   | U9 Yellow  | Michaels  | 034791212   | 
|  13 | Barry   | Dackers  | Harry   | U9 Yellow  | Dackers  | 034871996   | 
|  14 | Kevin   | Mitchell  | Daniel   | U9 Yellow  | Mitchell  | 034742886   | 
|  15 | Rebecca   | Mitchell  | Daniel   | U9 Yellow  | Mitchell  | 034742886   | 
+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+ 

如果有人可以幫助我在此我想非常感謝。對於那些想要用數據測試查詢的人,我已經將模式(DDL)和數據(DML)腳本(放在一個「粘貼」中)放在http://pastebin.com/S4iJyJUh處。

此外,如果有人認爲有更好的辦法我可以做的觀點,讓我知道。

+0

+1爲是精確的,提供DDL/DML,並使其易於爲您服務! – kba

回答

1

你幾乎在那裏,你只需要結合這兩個查詢。

CREATE VIEW phonelistWithTeams AS 
SELECT parent.parentID AS ParentID, 
    ParentPerson.firstName AS ParentFirstName, 
    ParentPerson.lastName AS ParentLastName, 
    ChildPerson.firstName AS PlayerFirstName, 
    ChildPerson.lastName AS PlayerLastName, 
    team.teamName AS TeamName, 
    GuardianHomePhone.homeNumber AS GuardianHomePhone 
FROM parent 
    JOIN player ON (parent.parentID IN (player.motherID, player.fatherID)) 
    JOIN person AS ParentPerson ON (ParentPerson.personID = parent.parentID) 
    JOIN person AS ChildPerson ON (ChildPerson.personID = player.playerID) 
    JOIN addressDetails AS GuardianHomePhone ON (GuardianHomePhone.personID = parent.parentID) 
    JOIN teamAllocation ON (ChildPerson.personID = teamAllocation.playerID) 
    JOIN team ON (teamAllocation.teamID = team.teamID); 

運行SELECT * FROM phonelistWithTeams會給你

+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+ 
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | PlayerLastName | TeamName | GuardianHomePhone | 
+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+ 
|  8 | Gregory   | Peck   | Michael   | Peck   | U10 Red | 034871234   | 
|  9 | Laura   | Peck   | Michael   | Peck   | U10 Red | 034871234   | 
|  10 | Martha   | Petersen  | Matt   | Petersen  | U11 Orange | 034724321   | 
|  10 | Martha   | Petersen  | Christopher  | Petersen  | U10 Red | 034724321   | 
|  11 | Chris   | Michaels  | Richard   | Michaels  | U11 Orange | 034791212   | 
|  11 | Chris   | Michaels  | Shaun   | Michaels  | U9 Yellow | 034791212   | 
|  12 | Nadine   | Michaels  | Richard   | Michaels  | U11 Orange | 034791212   | 
|  12 | Nadine   | Michaels  | Shaun   | Michaels  | U9 Yellow | 034791212   | 
|  13 | Barry   | Dackers  | Harry   | Dackers  | U9 Yellow | 034871996   | 
|  14 | Kevin   | Mitchell  | Daniel   | Mitchell  | U9 Yellow | 034742886   | 
|  15 | Rebecca   | Mitchell  | Daniel   | Mitchell  | U9 Yellow | 034742886   | 
+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+ 
+0

我疲憊的眼睛讓我忘記了AddressDetails表中有一個personID - 不應該忘記,因爲這是表主鍵。你的反饋完美無缺,只是希望我可以以某種方式將重複值組合在一起(例如「Martha Petersen」重複了兩次,因爲她有兩個「孩子/玩家」。我嘗試使用GROUP_CONCAT,但不知道如何在我的情況下實現它 - 理想情況下,我希望有名字和姓氏組合不重複,除非它是不可避免的 除此之外,你的答案是完美的,很高興你花時間幫助我:) – Rob

+0

如果你不想要瑪莎彼得森在那裏兩次,這將意味着馬特或克里斯托弗不會出現。那是你要的嗎? – kba

相關問題