MYSQL - The used SELECT statements have a Different number of Columns

Asked

Viewed 229 times

0

I have a Mail Query that returns a result that does not contain some data I need, like LOC_NU, LOC_NUM_SUB and MUN_NU.

Follow the original NO these fields and no errors:

select log_logradouro.ufe_sg, log_localidade.loc_no, log_bairro.bai_no, log_logradouro.tlo_tx+" " + log_logradouro.log_no as log_no, log_logradouro.cep, log_logradouro.log_complemento,"" as nome

from log_logradouro, log_localidade, log_bairro

where log_logradouro.loc_nu= log_localidade.loc_nu and log_logradouro.bai_nu_ini=log_bairro.bai_nu and log_logradouro.log_sta_tlo ="s"

union

select log_logradouro.ufe_sg, log_localidade.loc_no, log_bairro.bai_no, log_logradouro.log_no as log_no, log_logradouro.cep, log_logradouro.log_complemento,"" as nome

from log_logradouro, log_localidade, log_bairro

where log_logradouro.loc_nu= log_localidade.loc_nu and log_logradouro.bai_nu_ini=log_bairro.bai_nu and log_logradouro.log_sta_tlo ="n"

union

SELECT LOC.UFE_SG, LOC.LOC_NO AS LOC_NO, "" AS BAI_NO, "" AS LOG_NO, LOC.CEP, "" AS LOG_COMPLEMENTO, "" AS NOME

FROM LOG_LOCALIDADE AS LOC

WHERE LOC.CEP IS NOT NULL

AND LOC.LOC_NU_SUB IS NULL

UNION

SELECT LOC.UFE_SG, LOCSUB.LOC_NO AS LOC_NO, LOC.LOC_NO AS BAI_NO, "" AS LOG_NO, LOC.CEP, "" AS LOG_COMPLEMENTO, "" AS NOME

FROM LOG_LOCALIDADE AS LOC, LOG_LOCALIDADE AS LOCSUB

WHERE LOC.CEP IS NOT NULL

AND LOC.LOC_NU_SUB IS NOT NULL

AND LOC.LOC_NU_SUB= LOCSUB.LOC_NU

union

select log_cpc.ufe_sg, log_localidade.loc_no,"" as bai_no, log_cpc.cpc_endereco as log_no, log_cpc.cep,"" as log_complemento,cpc_no as nome

from log_cpc, log_localidade

where log_cpc.loc_nu=log_localidade.loc_nu

union

select log_grande_usuario.ufe_sg, log_localidade.loc_no, log_bairro.bai_no as bai_no, log_grande_usuario.gru_endereco as log_no, log_grande_usuario.cep,"" as log_complemento,gru_no as nome

from log_grande_usuario, log_localidade, log_bairro

where log_grande_usuario.loc_nu=log_localidade.loc_nu and log_grande_usuario.bai_nu = log_bairro.bai_nu

union

select log_unid_oper.ufe_sg, log_localidade.loc_no, log_bairro.bai_no as bai_no, log_unid_oper.uop_endereco as log_no, log_unid_oper.cep,"" as log_complemento, uop_no as nome

from log_unid_oper, log_localidade, log_bairro

where log_unid_oper.loc_nu=log_localidade.loc_nu and log_unid_oper.bai_nu = log_bairro.bai_nu;

Now what I tried to add new columns and presents me the error "The used SELECT statements have a Different number of Columns":

select log_logradouro.ufe_sg, log_localidade.LOC_NU, log_localidade.LOC_NU_SUB, log_localidade.loc_no, log_bairro.bai_no, log_localidade.MUN_NU, log_logradouro.tlo_tx+" " + log_logradouro.log_no as log_no, log_logradouro.cep, log_logradouro.log_complemento,"" as nome

from log_logradouro, log_localidade, log_bairro

where log_logradouro.loc_nu= log_localidade.loc_nu and log_logradouro.bai_nu_ini=log_bairro.bai_nu and log_logradouro.log_sta_tlo ="s"

union

select log_logradouro.ufe_sg, log_localidade.LOC_NU, log_localidade.LOC_NU_SUB,log_localidade.loc_no, log_bairro.bai_no, log_localidade.MUN_NU, log_logradouro.log_no as log_no, log_logradouro.cep, log_logradouro.log_complemento,"" as nome

from log_logradouro, log_localidade, log_bairro

where log_logradouro.loc_nu= log_localidade.loc_nu and log_logradouro.bai_nu_ini=log_bairro.bai_nu and log_logradouro.log_sta_tlo ="n"

union

SELECT LOC.UFE_SG, LOC.LOC_NU, LOC.LOC_NU_SUB, LOC.LOC_NO, LOC.MUN_NU AS LOC_NO, "" AS BAI_NO, "" AS LOG_NO, LOC.CEP, "" AS LOG_COMPLEMENTO, "" AS NOME

FROM LOG_LOCALIDADE AS LOC

WHERE LOC.CEP IS NOT NULL

UNION

SELECT LOC.UFE_SG, LOC.LOC_NU AS LOC_NU, LOC.LOC_NU_SUB AS LOC_NUM_SUB, LOCSUB.LOC_NO AS LOC_NO, LOC.LOC_NO AS BAI_NO, "" AS LOG_NO, LOC.CEP, "" AS LOG_COMPLEMENTO, "" AS NOME, LOC.LOC_NU AS MUN_NU

FROM LOG_LOCALIDADE AS LOC, LOG_LOCALIDADE AS LOCSUB

WHERE LOC.CEP IS NOT NULL

AND LOC.LOC_NU_SUB IS NOT NULL

AND LOC.LOC_NU_SUB= LOCSUB.LOC_NU

union

select log_cpc.ufe_sg, log_localidade.LOC_NU, log_localidade.LOC_NU_SUB, log_localidade.loc_no, log_localidade.MUN_NU,"" 
as bai_no, log_cpc.cpc_endereco as log_no, log_cpc.cep,"" as log_complemento,cpc_no as nome

from log_cpc, log_localidade

where log_cpc.loc_nu=log_localidade.loc_nu

union

select log_grande_usuario.ufe_sg, log_grande_usuario.LOC_NU, log_localidade.loc_no, log_bairro.bai_no as bai_no, log_grande_usuario.gru_endereco as log_no, log_grande_usuario.cep,"" as log_complemento,gru_no as nome

from log_grande_usuario, log_localidade, log_bairro

where log_grande_usuario.loc_nu=log_localidade.loc_nu and log_grande_usuario.bai_nu = log_bairro.bai_nu

union

select log_unid_oper.ufe_sg, log_localidade.LOC_NU, log_localidade.LOC_NU_SUB, log_localidade.loc_no, log_bairro.bai_no as bai_no, log_unid_oper.uop_endereco as log_no, log_unid_oper.cep,"" as log_complemento, uop_no AS nome, log_localidade.MUN_NU AS mun_nu

from log_unid_oper, log_localidade, log_bairro

where log_unid_oper.loc_nu=log_localidade.loc_nu and log_unid_oper.bai_nu = log_bairro.bai_nu;

I can’t identify what I should change to add the columns I need more from the original Query.

Thanks for your help.

  • To use UNION all expressions need to be union-compatible, that is to have the same amount of fields. It seems to me that this SELECT select log_grande_usuario.ufe_sg, log_grande_usuario.LOC_NU, log_localidade.loc_no, log_bairro.bai_no as bai_no, log_grande_usuario.gru_endereco as log_no, log_grande_usuario.cep, "" as log_complemento, gru_no as nome

from log_grande_usuario, log_localidade, log_bairro

where log_grande_usuario.loc_nu=log_localidade.loc_nu and log_grande_usuario.bai_nu = log_bairro.bai_nu has less fields than the others.

  • Thank you. That’s exactly what it was.

1 answer

0


When you use Unio the number of columns has to be the same, if different this error will occur even = error "The used SELECT statements have a Different number of Columns"

Browser other questions tagged

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