Here are my two tables
users
------------------------
id username name surname
2 Foo f b
4 Bar b f
orders
---------------------
id user_id price qty
1 2 3.2 1
2 4 6 4
etc ...
And here is how my query looks like
SELECT
u.name,
u.surname,
COUNT(r.user_id) as total,
SUM(r.price) as total_price,
FROM orders r
LEFT JOIN users u on u.id = r.user_id
WHERE order_id = 4
GROUP BY user_id
Thus I get total of money that user is spent.
The problem is that the calculation is wrong because the user can be bought more than one item of product.
I can’t figure out how to do so calculation to include and quanity.
You can try to multiply and sum, like :-