Sum of multiplication of columns for rows with similar IDs in MySQL

前端 未结 2 983
别那么骄傲
别那么骄傲 2021-01-05 02:15

I have 3 columns in a table called \"purchases\":

id         amount         price
2          2              21
2          5              9
3          8               


        
2条回答
  •  星月不相逢
    2021-01-05 02:52

    SELECT
    id, 
    SUM(amount*price) AS total
    FROM mytable
    GROUP BY id
    

    Data:

    | id | amount | price |
    |----|--------|-------|
    | 2  | 3      | 19    |
    | 2  | 3      | 89    |
    | 3  | 203    | 1     |
    

    Result:

    id  total
    2   324
    3   203
    

提交回复
热议问题