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.
1 answers
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;
Hot tools Tags
Hot Questions
Popular tool
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 phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
20522
7
13634
4






