How to make these SQL requests?

Asked

Viewed 64 times

1

I’m developing a real estate portal but as it’s the first time I do one, I don’t know exactly how the logic works to achieve the desired result. After plotting a path, I’ll still have to turn it into requisitions SQL.

As can be seen in the image, I have variants for TIPO, for DORMITORIOS, for AREA, for VAGAS, for VALOR, for BAIRROS, for CODIGO.

Not exactly looking for ready answers, I seek ideas on how to accomplish this task and I am open to increase information according to the doubts found.

Things I already know:

  • When typing a CODIGO all other fields will be disregarded ...
  • I think I can start filtering by VALOR, AREA ...

inserir a descrição da imagem aqui

  • 1

    It seems that TYPE, DORMITORIOS, AREA, VACANCIES, VALUE and NEIGHBORHOODS are the filters and CODE would be a certain ad. If CODE is reported, disregard filters. I believe filters are optional, so each field can take a value default in case of not being informed and when informed you use the value in QUERY.

1 answer

4

There is not much secret about how it should be done, the business rule of your screen is usually:

IF code is informed, the other fields should be ignored, then, SELECT * FROM tabela WHERE codigo = 'codigo', otherwise, every time a field comes with data, you add in your WHERE one AND too much, ex:

  • with type of dormitory:

    SELECT * FROM imovel WHERE tipo_de_dormitorio = 'tipoDeDormitorio'
    
  • with dormitory type and dorm numbers:

    SELECT * FROM imovel 
        WHERE tipo_de_dormitorio = 'tipoDeDormitorio' 
        AND numero_de_dormitorios = numeroDeDormitorios
    
  • with dormitory type, dorm numbers and number of vacancies:

    SELECT * FROM imovel
        WHERE tipo_de_dormitorio = 'tipoDeDormitorio' 
        AND numero_de_dormitorios = numeroDeDormitorios 
        AND numero_de_vagas = numeroDeVagas
    

This is basically speaking, after you put normalization in your bank, you only need to put the JOIN in the right places.

Browser other questions tagged

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