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
When you have more than one, which of the benefits is to return?
– Bacco
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.– mau humor
I expressed myself badly, it should return all benefits, but without repeating the name of the product,
– fabricio
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.
– Bacco
then I’ll have to use two selects ?
– fabricio
It is one of the outputs. Do one by taking the products, and one by taking the benefits, and join in PHP.
– Bacco
The structure you expect is like a node related to values, as SQL only returns a grid, a table.
– mau humor
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.
– Bacco
I put a picture of how my Ner returns
– fabricio
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– mau humor