SQL in DB Mysql

Asked

Viewed 55 times

2

I have the following situation, a register with 5 fields being them: Type1, Type2, Type3, Type4 and value

The 4 type fields are combos fields, where you can select the all option, or any other option.

To illustrate:

<select class="form-control" id="tipo1" name="tipo1">
    <option value="" selected>TODOS</option>
    <option value="1">Opção 1</option>
    <option value="3">Opção 2</option>
    ...
</select>
<select class="form-control" id="tipo2" name="tipo2">
...

So far so good, is working ok. In Mysql I have the following situation

Id  Tipo1  Tipo2  Tipo3  Tipo4  Valor
1   1      Null   2      3      100.00

Obs.:

  1. The 4 types cannot be null at the same time.
  2. It can happen that type 1, or Tipo2 or type 3 is filled and all other types are null.

To contextualize, this is a goal register, which depending on the situation and the value if it was exceeded, should pass, or block.

I am using PHP and Mysql, my problem is to make a query if the situation exists in the bank, I am not able to see how to do it. I do not know if I could be clear, and it may be that the answer is in front of me, but I have already broken my head enough and nothing came out, so I decided to ask for help. hehe.

  • I didn’t quite understand your question, you’re having trouble executing the query in the database and returning a certain situation, that’s it?

  • I think so, @Ricardopunctual. Scussel, please specify the "certain situation" you are looking for.

  • Do you want the query to fail if the 4 fields are Null or do you want to check this at the front/backend ? The problem is when to search or do not want to insert with the 4 nulls ?

1 answer

0

my problem is to make a query if the situation exists in the bank

Man from what I understand the only situation that can not occur is if the four fields are null at the same time, any other is accepted. It seems that you did not validate this before because then it would not make sense to make the consultation in the bank.

sql to check the cases in this 'wrong' situation would be:

select id from tabela 
where tipo1 is null 
and tipo2 is null 
and tipo3 is null 
and tipo4 is null

Remembering that

tipo1 is null (campo nulo)

is a different expression from

tipo1 = "" (string vazia)

Browser other questions tagged

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