Inner John repeating data

Asked

Viewed 1,234 times

1

I use a query using the Inner Join, only the problem is that it duplicates the results I have a table in mysql that is called produtos_destaque where the id_produtoDestaque is foreign key in the benefits table. My problem is that a product can have up to 5 benefits and at the time of showing the data in php all products that has more than 1 benefit is duplicated

SELECT produto_destaque.*,beneficio.beneficio 
FROM produto_destaque 
INNER JOIN beneficio ON produto_destaque.id_produtoDestaque = beneficio.id_produtoDestaque

I’ve tried using the group by and I was unsuccessful. How could I solve this problem ?

inserir a descrição da imagem aqui

this is the return you get from my consultation with the Internet

  • When you have more than one, which of the benefits is to return?

  • There is no way to take the product only once, and everything related to the product. A query will always return a table. GROUP BY works only with numbers, as it allows you to compile values in a single line through mathematical operations.

  • I expressed myself badly, it should return all benefits, but without repeating the name of the product,

  • Join is to join side A with side B, how is it going to return a benefit without product? There is not much logic to the question then.

  • then I’ll have to use two selects ?

  • It is one of the outputs. Do one by taking the products, and one by taking the benefits, and join in PHP.

  • The structure you expect is like a node related to values, as SQL only returns a grid, a table.

  • A VERY simple solution is to first take the list of products, and store in a array (if it’s not gigantic, of course). Then when you pick up each line, you add an extra member on the line called benefits, with empty array. Then make a select on the benefits, and already put in the right product array by the relationship id.

  • I put a picture of how my Ner returns

  • Maybe this solves here: http://stackoverflow.com/questions/276927/can-i-concatenate-multiplemysql-rows-into-one-field. Unlike what I said, it seems to use text concatenation operation with GROUP BY. DOC: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-Concat

Show 5 more comments

1 answer

5

The JOIN relates two tables, relating the data as if it were all part of a single table, composed of the two.

Here’s a good explanation of how it works:

What is the difference between INNER JOIN and OUTER JOIN?


If you need several fields of beneficios

In your case, if you want all the benefits, but only once each product, you can make this organization by PHP, something like this:

$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) die( $mysqli->connect_error );

// usei o * por nao saber seus campos. na pratica use só os que precisa
$mysqli->query( 'SELECT * FROM produto_destaque' ) or die($mysqli->error);
$res = $mysqli->use_result();

// criamos um array vazio para guardar os produtos
$produtos = array();
while ($row = $res->fetch_assoc()) {
    // em cada produto acrescentamos um lugar para os beneficios
    $row['beneficios'] = array();
    // e guardamos a linha em $produtos
    $produtos[$row['id_produtoDestaque']] = $row;
}
$res->free();

// em seguida, vamos pegar todos os beneficios
$mysqli->query( 'SELECT * FROM beneficio' ) or die($mysqli->error);
$res = $mysqli->use_result();

while ($row = $res->fetch_assoc()) {
    // agora guardamos o beneficio na chave 'beneficios' do 
    // produto correspondente, que criamos no SELECT anterior
    $produtos[$row['id_produtoDestaque']]['beneficios'][] = $row;
}
$res->free();

// aqui é só para conferir o resultado
print_r( $produtos );


If you need a single field

If you need a field of benefits only, as our colleague @mauhumor commented, you can use group_concat:

SELECT     produto_destaque.*, GROUP_CONCAT( beneficio.beneficio )
FROM       produto_destaque 
INNER JOIN beneficio
           ON produto_destaque.id_produtoDestaque = beneficio.id_produtoDestaque
GROUP BY   produto_destaque.id_produtoDestaque

Operation of group_concat

  • With the use of group_concat worked, but he returns me the results so - Sensação de saciedade, efeito parecido a sibutramina, Acelera a plenitude gástrica, Auxilia na manutenção do peso, Aumenta a queima de gordura, Reduz gordura visceral. It would have like it to return so only one below the other : - Sensação de saciedade, 
- efeito parecido a sibutramina, 
- Acelera a plenitude gástrica,
- Auxilia na manutenção do peso, 
- Aumenta a queima de gordura, 
- Reduz gordura visceral.

  • @Fabricio yes, see the link in the yellow block for more details of the function; If you specify the second parameter, you can put whatever you want in place of the comma (for example, a dot and comma and then the line break). The default separator is the comma, but you can put the string you want.

Browser other questions tagged

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