Mysql, SELECT and JOIN in tables with NULL columns

Asked

Viewed 35 times

0

Folks would like help with my comic.

I have three questions to answer and I can answer only the third, as I answer the others?

1) Which carriers are present in only 1 category?

2) Which carriers are present in 2 categories?

3) Which carriers are present in the three categories?

Thank you!

  • What you’ve tried that didn’t work out?

1 answer

0


This answer is not of a line but it works:

1) Which carriers are present in only 1 category?

SELECT t.Transportadora, v.Preferencia_Nacional_Voto, v.Transporte_Internacional_Voto, v.Transporte_Rodoaereo_Voto
FROM transportadora t
JOIN voto v on t.Id_Transportadora = v.Id_Transportadora
WHERE

(v.Preferencia_Nacional_Voto is null
and
v.Transporte_Internacional_Voto is not null
and
v.Transporte_Rodoaereo_Voto is not null)

OR 

(v.Preferencia_Nacional_Voto is not null
and
v.Transporte_Internacional_Voto is null
and
v.Transporte_Rodoaereo_Voto is not null)

OR 

(v.Preferencia_Nacional_Voto is not null
and
v.Transporte_Internacional_Voto is not null
and
v.Transporte_Rodoaereo_Voto is null);

2) Which carriers are present in 2 categories?

SELECT t.Transportadora, v.Preferencia_Nacional_Voto, v.Transporte_Internacional_Voto, v.Transporte_Rodoaereo_Voto
FROM transportadora t
JOIN voto v on t.Id_Transportadora = v.Id_Transportadora
WHERE

(v.Preferencia_Nacional_Voto is null
and
v.Transporte_Internacional_Voto is null
and
v.Transporte_Rodoaereo_Voto is not null)

OR 

(v.Preferencia_Nacional_Voto is null
and
v.Transporte_Internacional_Voto is not null
and
v.Transporte_Rodoaereo_Voto is null)

OR 

(v.Preferencia_Nacional_Voto is not null
and
v.Transporte_Internacional_Voto is null
and
v.Transporte_Rodoaereo_Voto is null);
  • Thank you, Edney, thank you! After I posted my doubt I was able to resolve with the support of my brother who is from IT, we follow the same reasoning changing only a few things but the answer was the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.