Delete record that is being duplicated in the query with the inner join

1

I am doing 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 handle the JOIN very well, and this is causing a small problem.

In my current database I am joining the table 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 since 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 due to my lack of experience, so I am not sure how to mount QUERY correctly!

I would then like to know how to avoid this duplication of values, or how to exclude all repeating 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    |
+----+---------+----------+

At the time of 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 just want to name the products, but I use filters and these filters go according to the characteristics I have in BD. And as far as I know, so I can make a WHERE ID_CARAC = '1' .

If I did a WHERE with a single condition it would be fine, because I would be returning only one element based on the example, however I can use various conditions, something like:

WHERE
    ID_CARAC = 1
    OR
    ID_CARAC = 2

And this causes the product "Pen" and "Pencil" to be listed more than once at the time of foreach , and for me it would be enough to display them only once.

  

How can I avoid this duplication, or how can I delete the repeated rows based on the product ID at the select time before even forming the final array and returning the result.

    
asked by anonymous 10.02.2017 / 07:07

2 answers

1

From what you have described, you only use the product to make a filter, so there is no need to show the features. It is therefore ideal that you do not use any type of JOIN , but the EXISTS clause in your WHERE :

SELECT prod.ID,
       prod.NOME
  FROM PRODUTOS prod
 WHERE EXISTS(SELECT rel.ID_CARAC
                FROM RELACAO rel
               WHERE rel.ID_PROD = prod.ID
                 AND rel.ID_CARAC IN (1, 2))

As you explained that in fact you do not show any feature information and just filter through them, a EXISTS will have the desired effect.

  

EXISTS Condition

     The MySQL EXISTS condition is used in combination with a subquery and is considered to be met if the subquery returns at least one row.

Free translation:

  

EXISTS condition

     

The MySQL EXISTS condition is used in combination with the subquery and is considered "to be met" if the subquery returns at least one record.

    
10.02.2017 / 11:43
0
  

My real goal is to list only the products, in fact I do not display these features on the screen, I just want to name the products, but I use filters ...

If I understood correctly, what you want is to list products that have certain characteristics, without duplicating them in the result.

select 

distinct A.id_prod
,B.nome

from relacao as A

left join produtos as B on B.id = A.id_prod

where A.id_carac in (1,2)
  

SQL Fiddle: link

    
10.02.2017 / 09:38