Select and add result of multiplying two columns from table in MySQL

Phi*_*hil 4 mysql sql

I'm trying to put together a MySQL query to select and multiply two columns from a specific table. In addition, I'd like the query to add the results of each multiplication if the value of a third column match any other rows.

The table has these columns: id, client_id, project_id, rate, and quantity.

So looking for rate multiplied by quantity for each row. Then add those results if every row has the same project_id.

这是我到目前为止所拥有的:

SELECT rate * quantity AS total_price FROM orders WHERE client_id=1 GROUP BY project_id

它似乎让我接近了,但并非完全如此。它似乎在一个大数组中返回所有计算结果,而不将相关的结果添加在一起。更复杂的是,有几个项目使用相同的client_id.

Pரத*_*ீப் 5

只需添加Sum聚合

SELECT project_id,
       Sum(rate * quantity) AS total_price
FROM   orders
WHERE  client_id = 1
GROUP  BY project_id 
Run Code Online (Sandbox Code Playgroud)