12
I have two tables in my bank and perform JOIN
with them. A table is a list of people and the other characteristic list of that person. By logic there is only one person and each person can have several characteristics, so two tables. So when I make one JOIN
appears the same person a few times only with its feature on the side. EX:
+-----+---------+----------------+
| ID | PESSOA | CARACTERÍSTICA |
+-----+---------+----------------+
| 1 | Carlos | Alto |
+-----+---------+----------------+
| 1 | Carlos | Divertido |
+-----+---------+----------------+
| 1 | Carlos | Inteligente |
+-----+---------+----------------+
| 2 | Iago | Baixo |
+-----+---------+----------------+
| 2 | Iago | Divertido |
+-----+---------+----------------+
| 2 | Iago | Esperto |
+-----+---------+----------------+
| 3 | Artur | Divertido |
+-----+---------+----------------+
| 3 | Artur | Inteligente |
+-----+---------+----------------+
If I do this SELECT
I get an empty result:
SELECT
p.*, c.*
FROM
pessoas AS p LEFT JOIN perfil AS c ON p.pid = c.perfil_pessoa
WHERE
c.caracteristica = 'Divertido' AND c.caracteristica = 'Inteligente'
When in fact I would like the following result:
+-----+---------+----------------+
| ID | PESSOA | CARACTERÍSTICA |
+-----+---------+----------------+
| 1 | Carlos | Alto |
+-----+---------+----------------+
| 1 | Carlos | Divertido |
+-----+---------+----------------+
| 1 | Carlos | Inteligente |
+-----+---------+----------------+
| 3 | Artur | Divertido |
+-----+---------+----------------+
| 3 | Artur | Inteligente |
+-----+---------+----------------+
That is, to have as a result, every person who is 'Divertida'
and 'Inteligente'
.
If in the clause WHERE
of SELECT
me use IN
or OR
the result is also not what I hope, by the example given, the SELECT
return all results, because all people have as characteristic "Fun":
+-----+---------+----------------+
| ID | PESSOA | CARACTERÍSTICA |
+-----+---------+----------------+
| 1 | Carlos | Alto |
+-----+---------+----------------+
| 1 | Carlos | Divertido |
+-----+---------+----------------+
| 1 | Carlos | Inteligente |
+-----+---------+----------------+
| 2 | Iago | Baixo |
+-----+---------+----------------+
| 2 | Iago | Divertido |
+-----+---------+----------------+
| 2 | Iago | Esperto |
+-----+---------+----------------+
| 3 | Artur | Divertido |
+-----+---------+----------------+
| 3 | Artur | Inteligente |
+-----+---------+----------------+
The purpose is to build filters using people’s characteristics.
The absence of the profile table model makes the response difficult.
– Luiz Sergio
It does not directly answer the question, but why not declare characteristic as a
SET
in the table persons? More information on: https://dev.mysql.com/doc/refman/8.0/en/set.html– Anthony Accioly