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

前端 未结 2 979
别那么骄傲
别那么骄傲 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
    
    0 讨论(0)
  • 2021-01-05 03:04

    @sombe: I've just tested your query and it works just fine. Are you sure your mysql is up to date?

    alt text

    The second works fine too:

    alt text

    0 讨论(0)
提交回复
热议问题