2011-06-08 16 views
12

Estoy ejecutando un informe que exporta la información para los miembros de los comités en una hoja de cálculo de Excel.Unirse si existe en una consulta MySQL

Aquí es mi consulta:

SELECT membership_organization.name AS Firm, 
membership_individual.first AS FirstName, 
membership_individual.middle AS MiddleName, 
membership_individual.last AS LastName, 
membership_individual.email AS Email, 
membership_individual.phone AS Phone, 
membership_location.addr1 AS Address1, 
membership_location.addr2 AS Address2, 
membership_location.city AS City, 
membership_location.state AS State, 
membership_location.zipcode AS Zip 
FROM membership_individual 
JOIN membership_organization ON membership_individual.org_name_id = membership_organization.id 
JOIN membership_location ON membership_individual.location_id = membership_location.id 
WHERE membership_individual.id IN ({list if ids}) 
ORDER BY LastName 

El problema es algunos de los miembros no tienen un conjunto de ID de ubicación, o no lo pone a 0, por lo que los miembros no aparecer en el informe.

¿Hay alguna manera de que califique la ubicación ÚNETE? Si la ID de ubicación de miembros existe, extraiga la información, si no me muestra la información que está disponible.

Respuesta

22

Cambie ambos de su JOIN s en LEFT JOIN s. Obtendrá todos sus registros de membership_individual donde coincide la cláusula where y los valores NULL para las otras tablas donde las filas no coinciden.

+0

Sí, pensé que era algo fácil, supongo que necesito un café. ¡Gracias! – Ryan

3

Use una combinación externa izquierda:

SELECT membership_organization.name AS Firm, 
membership_individual.first AS FirstName, 
membership_individual.middle AS MiddleName, 
membership_individual.last AS LastName, 
membership_individual.email AS Email, 
membership_individual.phone AS Phone, 
membership_location.addr1 AS Address1, 
membership_location.addr2 AS Address2, 
membership_location.city AS City, 
membership_location.state AS State, 
membership_location.zipcode AS Zip 
FROM membership_individual 
JOIN membership_organization ON membership_individual.org_name_id = membership_organization.id 
LEFT OUTER JOIN membership_location ON membership_individual.location_id = membership_location.id 
WHERE membership_individual.id IN ({list if ids}) 
ORDER BY LastName 
+2

'outer' está implícito. 'left join' es todo lo que es necesario. – Fosco

+0

@Fosco, a diferencia de tu comentario ... – Blindy

0

Intente utilizar

LEFT JOIN membership_location ON membership_individual.location_id = membership_location.id 

en lugar de su variante ..

0

Utilice la izquierda se unen, es decir, su consulta se convertirá en

SELECT membership_organization.name AS Firm, 
membership_individual.first AS FirstName, 
membership_individual.middle AS MiddleName, 
membership_individual.last AS LastName, 
membership_individual.email AS Email, 
membership_individual.phone AS Phone, 
membership_location.addr1 AS Address1, 
membership_location.addr2 AS Address2, 
membership_location.city AS City, 
membership_location.state AS State, 
membership_location.zipcode AS Zip 
FROM membership_individual 
JOIN membership_organization ON membership_individual.org_name_id = membership_organization.id 
LEFT JOIN membership_location ON membership_individual.location_id = membership_location.id 
WHERE membership_individual.id IN ({list if ids}) 
ORDER BY LastName 

y obtendrá los datos de todos los miembros, incluso aquellos para los que no hay filas en la tabla de ubicación.

Cuestiones relacionadas