search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

1 answers
Q&A Calculate the sum of fields in another table using MySQL SQL query

0

I have a schema like this: user table with attributes "user_id" and "username" and an orders table with attributes "customer_id" (FK of user_id) and "finalPrice" database schema I want to get the user with the highest price among all order combinations (basically the sum of all FinalPrice values ​​for the order, where customer_id = user_id) Can't quite figure out a solution so any support will be appreciated

select sum( (select o.final_price from `order` o where u.user_id=o.customer_id)) 
from user u group by u.user_id

is what I tried, but I keep getting the "Subquery returned more than 1 row" error message. Tried reading the documentation, but I'm still very inexperienced with SQL.

Your Answer
submit

1 answers
0

Based on your query You can't use SUM function like this but use internal SUM that's why it throws 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;
2024-04-07 17:24:28

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use