Add items from table 1 and bring rows indexed by table 2 code

1

I am very new to SQL and I am creating an application that brings the number of hours contracted, well the part that I have doubts in the query is the index of the lines ...

I need the rows to not repeat the code from table 1 and add the items from table 2.

My code is this:

SELECT tabela1.codigo_tabela2, SUM(tabela1.itens), tabela2.nome, tabela2.codigo
    FROM tabela1, tabela2
    WHERE tabela1.codigo_tabela2 = tabela2.codigo AND tabela1.ativo = b'1' AND tabela1.usuario = x

Well, I hope you can help me ... I could not find anything in my research, (it's very specific).

    
asked by anonymous 13.10.2014 / 15:57

1 answer

1
SELECT 
    tabela2.nome, 
    tabela2.codigo,
    SUM(tabela1.itens)
FROM tabela1, tabela2
WHERE 
    tabela1.codigo_tabela2 = tabela2.codigo AND 
    tabela1.ativo = b'1' AND  
    tabela1.usuario = x
GROUP BY
    tabela2.nome, 
    tabela2.codigo 
    
13.10.2014 / 17:29