SQL with 2 of multiple conditions

Asked

Viewed 502 times

2

I’m having trouble making a Select that meets 2 or 3 conditions of several selected, for example: Conditions:

- Cidade Tal (São Paulo)
- Nome Tal (João)
- Sexo (Masculino)
- Fumante (Não)
- Carro Próprio (Sim)

I want you to come back to me all of you who meet TWO or THREE of the conditions, depending on the moment I want you to meet TWO of them, and at other times I want three of them, but I would like a solution other than making combinations of OR and...

any other way than: ((Cidade AND Nome) OR (Cidade AND Sexo) OR (Cidade AND Fumante) OR (Cidade AND Carro) OR (Nome AND Sexo) OR (Nome AND Fumante) OR (Nome AND Carro) OR (Sexo AND Fumante) OR (Sexo AND Carro) OR (Fumante AND Carro))

And it gets worse with more options and when I want three questions instead of two....

------------ Tabela do Banco ------------
ID Nome  Estado Sexo Fuma Transporte Idade
1  Joao  RJ     Masc Sim  Sim        35
2  Maria SC     Fem  Sim  Sim        27
3  Jose  RJ     Masc Nao  Sim        23
4  Ana   SP     Fem  Nao  Nao        19
5  Carla SP     Fem  Nao  Sim        26
6  Bia   RJ     Fem  Nao  Nao        39
7  Edu   RJ     Masc Sim  Nao        18
8  Fabio SC     Masc Nao  Sim        31
...

I would like a result that brings me only who meets 3 of the specification: Estado: RJ Sexo: Feminino Fuma: Nao Transporte: Sim Idade: >30

------------ Tabela Resultado ------------
ID Nome  Estado Sexo Fuma Transporte Idade
1  Joao  RJ     Masc Sim  Sim        35 (Estado: RJ & Transporte: Sim & idade >30)
3  Jose  RJ     Masc Nao  Sim        23 (Estado: RJ & Fuma: Nao & Transporte: Sim)
5  Carla SP     Fem  Nao  Sim        26 (Sexo: Fem & Fuma: Nao & Transporte: Sim)
6  Bia   RJ     Fem  Nao  Nao        39 (Estado: RJ & Sexo: Fem & Fuma: Nao & Idade >30 "essa atende a 4 dos requisitos")
8  Fabio SC     Masc Nao  Sim        31 (Fuma: Nao & Transporte: Sim & Idade >30)
...

Some light at the end of the tunnel?

  • I believe that a query counting the records in a temporary table should solve your problem. At the moment I have no time to describe in detail a reply. As soon as I can form a query with the possible result.

2 answers

1


You can create a temporary table and count how many records meet each condition.

DROP TEMPORARY TABLE IF EXISTS `tmp_condicao`;
CREATE TEMPORARY TABLE tmp_condicao(
  id INT NOT NULL primary key,
  qtd INT NOT NULL
);

INSERT INTO tmp_condicao(id, qtd)
SELECT
  id,
  0
FROM minhaTabela;

UPDATE tmp_condicao AS A
INNER JOIN minhaTabela AS B
  ON (A.id = B.id)
SET A.qtd = A.qtd + 1
WHERE B.estado = 'RJ';

SELECT
*
FROM tmp_condicao;

For every new condition you have, just repeat the UPDATE and add the WHERE you want to add.

Finally, to search for records that have 3 conditions or more you can do:

SELECT
B.*
FROM tmp_condicao AS A
INNER JOIN minhaTabela AS B
  ON (A.id = B.id)
WHERE A.qtd >= 3;
  • 1

    Very good, thanks for your help.

0

Well, you can concatenate your query.

example:

$query = "SELECT * from table WHERE 1 = 1";
if ($nome)
{
    $query .= "Nome = ". $nome;
}
if ($cidade)
{
    $query .= "Cidade = ". $cidade;
}

This is just an example. I suggest using PDO for security reasons. :)

Browser other questions tagged

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