dag roger stokland rui
dag roger stokland rui

Reputation: 173

MySQL where clause with null values

I have this sql statement which looks like this:

SELECT hovedenhet.organisasjonsnummer, adresse.adresse , poststed.poststed, land.navn, kontaktdata.epostadresse
FROM hovedenhet 
LEFT JOIN adresse on hovedenhet.organisasjonsnummer = adresse.organisasjonsnummer
LEFT JOIN poststed on poststed.poststed_id=adresse.poststed_id
LEFT JOIN land on land.land_id = adresse.land_id
LEFT JOIN kontaktdata on kontaktdata.organisasjonsnummer = hovedenhet.organisasjonsnummer
#where (adresse.adresseType=1)

I want to include rows even though some of the values are null and this works fine with this code, but when i uncomment the where clause it doesnt include the null columns anymore. I have tried to do where (adresse.adresseType = 1 or adresse =null) but it had no effect. Is there any solution to this?

Upvotes: 2

Views: 98

Answers (3)

dag roger stokland rui
dag roger stokland rui

Reputation: 173

Currently using:

   SELECT hovedenhet.organisasjonsnummer, adresse.adresse , poststed.poststed, 
    land.navn, kontaktdata.epostadresse
    FROM hovedenhet 
    LEFT JOIN adresse on hovedenhet.organisasjonsnummer = adresse.organisasjonsnummer 
AND adresse.adresseType=1
    LEFT JOIN poststed on poststed.poststed_id=adresse.poststed_id
    LEFT JOIN land on land.land_id = adresse.land_id
    LEFT JOIN kontaktdata on kontaktdata.organisasjonsnummer = hovedenhet.organisasjonsnummer

Thanks for the answers!

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

Try this:

SELECT hovedenhet.organisasjonsnummer, adresse.adresse , poststed.poststed, land.navn, kontaktdata.epostadresse
FROM hovedenhet 
LEFT JOIN adresse on hovedenhet.organisasjonsnummer = adresse.organisasjonsnummer
LEFT JOIN poststed on poststed.poststed_id=adresse.poststed_id
LEFT JOIN land on land.land_id = adresse.land_id
LEFT JOIN kontaktdata on kontaktdata.organisasjonsnummer = hovedenhet.organisasjonsnummer
where (adresse.adresseType = 1 or adresse.adresseType is null)

Upvotes: 4

Zhivko Delchev
Zhivko Delchev

Reputation: 390

Try using

where (adresse.adresseType = 1 or adresse is null)

Upvotes: 1

Related Questions