2012-05-08 10 views
5

Sono riuscito a creare una vista da alcuni dati nel mio database MySQL, come di seguito:Come progettare una "vista" in MySQL che otterrà i risultati che sto cercando?

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> 

creazione di questa vista è stata una sfida, ma la descrizione di seguito è quello che sto avendo problemi durante la navigazione in giro:

Ho anche bisogno di aggiungere la squadra in cui ogni giocatore è in questa vista. Perché ottenere il nome del team per abbinare richiede di unire 4 tabelle insieme, ho difficoltà a unire le cose insieme. Sono riuscito a creare una query separata che corrisponde ai giocatori di squadre di seguito:

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> 

L'esito sto visualizzare nella mia testa è qualcosa di simile:

+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+ 
| 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   | 
+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+ 

Se qualcuno mi può aiutare su questo vorrei sii molto grato Per quelli di voi che vogliono testare le query con i dati ho inserito gli script (DDL) e dati (DML) dello schema (in una "incolla") a http://pastebin.com/S4iJyJUh.

Inoltre, se qualcuno pensa che ci sia un modo migliore, posso fare la vista, fammi sapere.

+0

+1 per essere precisi, fornendo DDL/DML, e rendendo più semplice per aiutarvi! – kba

risposta

1

Sei quasi arrivato, devi solo combinare le due query.

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); 

Esecuzione SELECT * FROM phonelistWithTeams vi darà

+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+ 
| 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

I miei occhi stanchi mi hanno fatto dimenticare che avevo un personID nella tabella AddressDetails - non dovrei aver dimenticato dato che questa è la chiave primaria delle tabelle. Il tuo feedback funziona perfettamente, vorrei solo riuscire a raggruppare insieme i valori ripetuti (ad es. "Martha Petersen" è ripetuto due volte perché ha 2 "bambini/giocatori". Ho provato a usare GROUP_CONCAT ma non sono sicuro di come implementarlo nella mia situazione - idealmente mi piacerebbe avere combinazioni di nome e cognome non ripetute, a meno che non sia inevitabile A parte questo la tua risposta è perfetta e felice che tu abbia preso il tempo di darmi una mano :) – Rob

+0

Se non vuoi Martha Petersen essere lì due volte, significherebbe che Matt o Christopher non si presenteranno. E 'questo quello che vuoi? – kba

Problemi correlati