Home > OS >  MySQL SQL query for sum of fields from another table
MySQL SQL query for sum of fields from another table

Time:06-20

I a have a schema like this: user table with attributes "user_id" and "username" and order table with attributes "customer_id"(being FK for user_id) and "finalPrice" enter image description here

CodePudding user response:

As per your query You cannot use a SUM function like that, instead use the SUM inside, that is why it throwing error like Subquery returns more than 1 row

SELECT  
u.user_id,
(SELECT SUM(o.final_price) FROM `order` o WHERE u.user_id=o.customer_id)
FROM 
user u 
GROUP BY u.user_id;
  • Related