INNER JOIN sql query with WHERE and GROUP BY

Asked

Viewed 2,450 times

0

I have this query in my database only that I wanted to insert a sum in a.preco_produto and do to GROUP BY only if I put GROUP BY a.codigo_loja in this query of this error> #1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near 'GROUP BY stores' at line 7

SELECT
a.codigo_loja_produto, a.codigo_loja, a.codigo_produto, b.nome_produto, b.codigo_barras, b.secoes_produtos, b.endereco_fotos,a.preco_produto,a.ultimo_preco, a.ultima_atualizacao
FROM loja_produto AS a
INNER JOIN  produtos AS b  on a.codigo_produto = b.cod_produto
INNER JOIN  lojas AS c on a.codigo_loja = c.cod_loja
WHERE a.codigo_loja = 3 Limit 20 OFFSET 0
GROUP BY a.codigo_loja
  • 2

    Can you put the group by what you’re using and what the mistake is otherwise difficult to help you

1 answer

3


It is necessary to group all columns with equal types.

Ex:

SELECT 
    a.codigo_loja_produto, 
    a.codigo_loja, 
    a.codigo_produto, 
    b.nome_produto, 
    b.codigo_barras, 
    b.secoes_produtos, 
    b.endereco_fotos,
    a.preco_produto,
    a.ultimo_preco, 
    a.ultima_atualizacao 
FROM loja_produto AS a 
    INNER JOIN produtos AS b ON a.codigo_produto = b.cod_produto 
    INNER JOIN lojas AS c ON a.codigo_loja = c.cod_loja 
WHERE a.codigo_loja = 3 
GROUP BY 
    a.codigo_loja_produto, 
    a.codigo_loja 
LIMIT 20 
OFFSET 0`

Taking into account that the product store code and store code contains equal records, to do this you must enter all columns that have identical record.

Browser other questions tagged

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