WHERE Clause only IF NOT NULL

Viewed 31068

I need help with my SELECT.

I got a field that can be NULL and in it there is stored a foreign-key.

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

So if it's NULL nothing happens.

How can I check if beerID is NOT NULL so I can use "beerBrandID = beerID"?

5 Answers

How about using with CASE statement in where clause like

WHERE
CASE WHEN  beer.id IS NOT NULL 
    THEN beer.brand_id = 12345
    ELSE TRUE
END
Related