Search multiple tables at once

0

Colleagues.

I need to search multiple tables (a total of 28) where each table corresponds to a state in Brazil. This search would be from the CEP, which is one of the fields of these tables. Inner Join I do not know if it would be the solution, otherwise I would have to give inner in all the tables. Would you have any other solution?

    
asked by anonymous 18.01.2017 / 18:31

2 answers

1

At Wikipedia has information how the structure of a zip code works:

States

  • 0xxxx:
    • Greater São Paulo (01000-09999)
  • 1xxxx:
    • Interior and coastline of São Paulo (11000-19999)
  • 2xxxx:
    • Rio de Janeiro (20000-28999)
    • Holy Spirit (29000-29999)
  • 3xxxx:
    • Minas Gerais (30000-39990)
  • 4xxxx:
    • Bahia (40000-48999)
    • Sergipe (49000-49999)
  • 5xxxx:
    • Pernambuco (50000-56999)
    • Alagoas (57000-57999)
    • Paraíba (58000-58999)
    • Rio Grande do Norte (59000-59999)
  • 6xxxx:
    • Ceará (60000-63990)
    • Piauí (64000-64990)
    • Maranhão (65000-65990)
    • Pará (66000-68890)
    • Amapá (68900-68999)
    • Amazon (69000-69299, 69500-69999)
    • Acre (69400-69499)
    • Roraima (69300-69399) 7xxxx: - Distrito Federal (70000-73699)
    • Goiás (73700-76799)
    • Rondônia (76800-76999)
    • Tocantins (77000-77999)
    • Mato Grosso (78000-78899)
    • Mato Grosso do Sul (79000-79999)
  • 8xxxx:
    • Paraná (80000-87999)
    • Santa Catarina (88000-89999) 9xxxx:
    • Rio Grande do Sul (90000-99999)

Components

X____-___: Região
_X___-___: Sub-região
__X__-___: Setor
___X_-___: Subsetor
____X-___: Divisor de subsetor
_____-XXX: Sufixo de distribuição

With this data it is evident that you just have to get the first digit of the zip code and you already have the information of which table in the DB it can be.

    
18.01.2017 / 18:54
1

I created a new response because it took a completely different approach.

I checked the database you are using and with two queries you can return the ZIP code's address.

  • A query to find out what status the zip code is
  • Another query to find out what city and street is.
  • Step 1

    -- Exemplo: CEP 97060-003
    SELECT 
        uf.UF,
        uf.Nome
    FROM uf
    WHERE "97060" BETWEEN uf.Cep1 AND uf.Cep2;
    
    +----+-------------------+
    | uf |       Nome        |
    +----+-------------------+
    | RS | Rio Grande do Sul |
    +----+-------------------+
    

    Step 2

    Save the UF field to a variable and do the next search

    -- Exemplo: CEP 97060-003
    SELECT *
    FROM rs as uf
    WHERE uf.cep = "97060-003"
    
    +-------+-------------+--------------------------+--------+-----------+---------------+
    |  id   |   cidade    |        logradouro        | bairro |    cep    | tp_logradouro |
    +-------+-------------+--------------------------+--------+-----------+---------------+
    | 20410 | Santa Maria | Nossa Senhora Medianeira | Centro | 97060-003 | Avenida       |
    +-------+-------------+--------------------------+--------+-----------+---------------+
    

    I do not know what language you're programming in, but it's very simple. The first search is done with the first 5 digits (% with%). While the second is done with all the digits and with the hyphen ( 99999 ).

        
    18.01.2017 / 21:13