How to make a Mysql SELECT by summing values of several Ids from a table?

Asked

Viewed 108 times

0

Hello!

I have two tables: one where I register entries and another where I have a register of payment methods. When I enter a value into the input table, I put the Pgto form ID and that ID corresponds to my payment method table ID.

My SELECT code is this:

SELECT SUM(r_entradas.entrada_valor) AS Valor,c_forma_pgto.forma_nome AS Forma FROM r_entradas
INNER JOIN c_forma_pgto ON r_entradas.forma_id = c_forma_pgto.forma_id
WHERE log_data = CURDATE()
ORDER BY r_entradas.forma_id ASC

I am trying to filter this sum so that it shows differently the respective sum of each ID. For example:

80 | Money

50 | Credit

Only in my current code, it ends up assigning some form and showing the total. Ex:

130 | Credit

1 answer

0


You need to use GROUP BY for the sum to occur according to each payment method. Example:

SELECT 
   SUM(r_entradas.entrada_valor) AS Valor, 
   c_forma_pgto.forma_nome AS Forma 
  FROM r_entradas
 INNER JOIN c_forma_pgto ON r_entradas.forma_id = c_forma_pgto.forma_id
 WHERE log_data = CURDATE()
 GROUP BY c_forma_pgto.forma_nome
 ORDER BY r_entradas.forma_id ASC
  • It worked perfectly, thank you very much!

Browser other questions tagged

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