2012-05-08 6 views
5

He conseguido crear una vista de algunos datos en mi base de datos MySQL, como a continuación:¿Cómo diseñar una "vista" en MySQL que obtenga los resultados que estoy buscando?

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> 

la creación de este punto de vista fue un reto, pero la descripción a continuación es lo que yo estoy teniendo problemas para navegar por:

También necesito agregar el equipo de cada jugador con esta vista. Debido a que hacer coincidir el nombre del equipo requiere unir 4 mesas, tengo problemas para fusionar las cosas. Me las arreglé para crear una consulta independiente que coincide con los jugadores a los equipos a continuación:

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> 

El resultado estoy visualizando en mi cabeza es algo como esto:

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

Si alguien me puede ayudar en esto lo haría se muy agradecido Para aquellos de ustedes que quieran probar las consultas con los datos, he puesto los scripts de esquema (DDL) y de datos (DML) (en un solo "pegar") al http://pastebin.com/S4iJyJUh.

Además, si alguien piensa que hay una mejor manera de hacerlo, avíseme.

+0

+1 por ser precisos, el suministro de DDL/DML, y lo que es fácil para ayudarle! – kba

Respuesta

1

Ya casi está allí, solo necesita combinar las dos consultas.

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

Correr SELECT * FROM phonelistWithTeams le 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

Mis ojos cansados ​​me hicieron olvidar que tenía una ID de persona en la tabla AddressDetails, no debería haber olvidado ya que esa es la clave principal de las tablas. Tus comentarios funcionan perfectamente, solo desearía poder agrupar de alguna manera los valores repetidos (por ejemplo, "Martha Petersen" se repite dos veces porque tiene 2 "hijos/jugadores". Intenté usar GROUP_CONCAT pero no estoy seguro de cómo implementarlo en mi situación - idealmente me gustaría tener combinaciones de nombre y apellido no repetidas, a menos que sea inevitable. Aparte de eso, tu respuesta es perfecta y me alegro de que te tomaste el tiempo para ayudarme :) – Rob

+0

Si no quieres Martha Petersen estar allí dos veces, eso significará que Matt o Christopher no aparecerán. ¿Es eso lo que quieres? – kba

Cuestiones relacionadas