我需要有关我的SELECT的帮助。
我得到了一个可以NULL的字段,其中存储了一个外键。

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

所以如果它是NULL什么都不会发生。
如何检查beerID是否是NOT NULL以便我可以使用"beerBrandID = beerID"

最佳答案

如果要包括没有匹配项的记录,则需要外部联接

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

关于mysql - WHERE子句仅在IF NOT NULL时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11971554/

10-08 20:44