1
I am making a query to my database and in this query I use an INNER JOIN to join two tables!
But I have a problem, because I do not know how to manipulate JOIN very well, and this is generating a small problem.
In my current bank I am uniting the table relation with product, so the union is from N to 1, and even if it was from 1 to N the result would be the same.
The problem is that as the union is being made from N values to 1, the product is being duplicated N times, if the product in question has 10 characteristics the product will be duplicated 10 times.
I believe this is caused by my lack of experience, so I am not able to mount the QUERY correctly!
I would then like to know how to avoid this duplication of values, or how to delete all the repeated products and leave only one.
This is a small example of the tables.
+----------------------+
| PRODUTOS |
+----+-----------------+
| ID | NOME |
+----+-----------------+
| 1 | Caneta |
+----+-----------------+
| 2 | Lapiseira |
+----+-----------------+
+----------------------+
| CARACTERISTICAS |
+----+-----------------+
| ID | VALOR |
+----+-----------------+
| 1 | AZUL |
+----+-----------------+
| 2 | NOVA |
+----+-----------------+
| 3 | BIC |
+----+-----------------+
| 4 | ESCREVE |
+----+-----------------+
| 5 | 10G |
+----+-----------------+
+-------------------------+
| RELACAO |
+----+---------+----------+
| ID | ID_PROD | ID_CARAC |
+----+---------+----------+
| 1 | 1 | 1 |
+----+---------+----------+
| 2 | 1 | 2 |
+----+---------+----------+
| 3 | 1 | 3 |
+----+---------+----------+
| 4 | 1 | 4 |
+----+---------+----------+
| 5 | 1 | 5 |
+----+---------+----------+
| 6 | 2 | 1 |
+----+---------+----------+
| 7 | 2 | 2 |
+----+---------+----------+
| 8 | 2 | 3 |
+----+---------+----------+
| 9 | 2 | 4 |
+----+---------+----------+
| 10 | 2 | 5 |
+----+---------+----------+
When listing the result, the product name appears the same amount of times as the feature number it has!
+------------------------------+
| RESULTADO |
+-------+----------+-----------+
|IDPROD | ID_CARAC | NOME |
+-------+----------+-----------+
| 1 | 1 | Caneta |
+-------+----------+-----------+
| 1 | 2 | Caneta |
+-------+----------+-----------+
| 1 | 3 | Caneta |
+-------+----------+-----------+
| 1 | 4 | Caneta |
+-------+----------+-----------+
| 1 | 5 | Caneta |
+-------+----------+-----------+
| 2 | 1 | Lapiseira |
+-------+----------+-----------+
| 2 | 2 | Lapiseira |
+-------+----------+-----------+
| 2 | 3 | Lapiseira |
+-------+----------+-----------+
| 2 | 4 | Lapiseira |
+-------+----------+-----------+
| 2 | 5 | Lapiseira |
+-------+----------+-----------+
My real goal is to list only the products, in fact I do not display these features on the screen, I list only the name of the products, but I use filters and these filters go according to these features I have in BD. And as far as I know, so I can make one WHERE ID_CARAC = '1'
.
If I did a WHERE with a single condition would be fine, because I would be returning only one element based on the example, but I can use several conditions, something like:
WHERE
ID_CARAC = 1
OR
ID_CARAC = 2
And that makes it at the time of foreach
the product "Pen" and "Pencil" is listed more than once, and for me it would be enough to display them only once.
How can I avoid this duplication, or how can I delete repeated lines based on the product ID at select time before I even form the final array and return the result.
Post here ALL the query you are using today, not just the WHERE clause.
– Thiago Lunardi
Experiment with... GROUP BY products.id
– user1915816
Looking at the question would see the possibility of taking this ID_CARAC from the columns returned by the query and using DISTINCT not to take duplicate rows.
– Antonio Alexandre