2012-08-15 10 views
7

Necesito ayuda con mi SELECT.WHERE Cláusula solamente IF NOT NULL

Tengo un campo que puede ser NULL y en él se almacena una clave externa.

SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerBrandID = beerID <--- this can be NULL 

Así que si es NULL no pasa nada.

¿Cómo puedo comprobar si beerID es NOT NULL entonces puedo usar "beerBrandID = beerID"?

+7

+1 porque tiene una tabla llamada beerImage – goat

+0

Podría aclarar su problema un poco? Tal como lo veo, en la tabla 'beerImage' hay un campo' beerId' que se usa como clave foránea, pero es nulo, ¿verdad? ¿Necesita recopilar los datos de dos tablas a la vez? – raina77ow

+0

Que sea un +2 para el nombre de la tabla beerImage, usted tiene mi voto. – Michael

Respuesta

2

Si desea incluir registros donde no hay ninguna coincidencia, se necesita una combinación externa

SELECT beer.id AS beerID, 
beer.barrelID AS barrelID, 
beer.imageID AS imageID, 
beer.title AS title, 
beer.map AS map, 
beer.longitude AS longitude, 
beer.latitude AS latitude, 
brand.name AS brandName, 
brew.type AS brewType, 
image.name AS imageName, 
variation.name AS variationName 
FROM brand, brew, image, beer 
LEFT OUTER JOIN variation ON variation.ID = beer.VariationID 
WHERE beer.id = %s 
AND md5(beer.memberID) = %s 
AND beer.review = 1 
AND brand.ID = beer.brandID 
AND brew.ID = beer.brewID 
AND image.ID = beer.imageID 
+0

esto causa Columna desconocida 'beer.VariationID' en 'on clause' – user1600867

+0

intente mover la cerveza hasta el final de – FJT

+0

¡¡¡Esto es, GRACIAS MUCHO !!!! – user1600867

0

Para comprobar NULL/no es nulo, el uso ES NULO/no es nulo

AND beerID IS NOT NULL 
+0

DÓNDE beerBRewID = brewID Y beerBrandID = beerID Y beerID NO ES NULL como este? – user1600867

7

es probable que tenga algo como esto:

Primer ejemplo:

SELECT * FROM beerImage WHERE beerBRewID = brewID AND (beerID IS NULL OR beerBrandID = beerID) 

Segundo ejemplo:

SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerID IS NOT NULL AND beerBrandID = beerID 

El primer ejemplo le permitirá mostrar registros que tienen nulo de beerID junto con las cervezas que beerBrandID es igual a beerID (ambos).

El segundo devolverá exactamente las cervezas que corresponden a beerBrandID (con exclusión de NULL IDs).

+0

Si beerBrandId = beerId, beerId definitivamente no es nulo. – raina77ow

+0

Luego use el segundo ejemplo. – Rolice

+0

Esto es lo que probé, pero establece el campo en 1 y selecciona la variación incorrecta. WHERE beer.id =% s AND md5 (beer.memberID) =% s Y beer.review = 1 Y brand.ID = beer.brandID AND brew.ID = beer.brewID AND image.ID = beer.imageID AND (beer.variationID IS NULL O variation.ID = beer.variationID) – user1600867

0

Puede usar las funciones de comparación MySQL "IS NULL" o "IS NOT NULL".

Leer más sobre esto aquí: http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_is-null

+0

Esto es mi SELECCIONAR: SELECCIONAR beer.id AS beerID, beer.barrelID AS barrelID, beer.imageID AS imageID, beer.title AS title, beer'map' AS map, beer.longitude AS lengthitud, beer.latitude AS latitude, brand.name AS nombre de marca, brew.type AS brewType, image.name AS nombre de imagen, variante.nombre AS variationName FROM cerveza, marca, brew, imagen, variación WHERE beer.id =% s AND md5 (beer.memberID) =% s AND beer.review = 1 AND brand.ID = beer.brandID AND brew.ID = beer.brewID AND image.ID = beer.imageID Y variation.ID = beer.variationID beer.variationID esto puede ser NULL, si es NULO nada será b e seleccionado – user1600867