locked
Mysql Total from 2 tables RRS feed

  • Question

  • User-909867351 posted

    Hi 

    This question  is for one Mysql database

    I have one table (named contas)  with the fields 

    id | nome | conta

    and other table (named movimentos) with the fields

    id | tipo | conta | valor

    I need the sum(valor) from the table movimentos for different values (group by?) of conta from table contas

    Thank you

    Wednesday, November 11, 2015 6:18 AM

Answers

  • User269602965 posted

    had to guess these two tables are joined on the key = id  1 for 1 (not normal primary key > foreign key one to many relationship)

    SELECT
      con.conta,
      SUM(mov.valor) as SUM_VALOR
    FROM
      contas con
    INNER JOIN movimentos mov ON con.id = mov.id
    GROUP BY
      con.conta
    /

    Too bad MySQL does not yet support Analytic functions to make windows (partitions) of data and then apply various things like sum, count, avg, etc. to your windows.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2015 1:47 PM