Add items from table 1 and bring in indexed Rows from table 2

Asked

Viewed 78 times

1

I am very new with 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 lines...

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

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 couldn’t find anything in my research, (it’s very specific).

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 
  • Thanks, I didn’t know that statement "GROUP BY". :D Ah! I removed the code2 from SELECT, but kept it in GROUP BY @mxix

Browser other questions tagged

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