Sample Header Ad - 728x90

Select tables data with sum of other tables

0 votes
1 answer
181 views
I am very new on MySQL DBA and i searched but i am not clearly understanding inner join with sum. user table: id, username, locationid history table: id, username, locationid, spent Example user table: 1, sarah12, 12 2, martin21, 12 3, sarah12, 13 Example history table: 1, sarah12, 12, 100 2, martin21, 12, 40 3, sarah12, 13, 500 4, sarah12, 12, 50 Now i want to list all users with sum of their spending history by specific location ID. Like i want to list all users which has locationid 12 with sum of their spending history So, i am expecting something like that: 1, sarah12, 150 2, martin21, 40 Can you please guide me how can i do that? Edit: Basically i try with random solution from stackoverflow. I have added my last tried code. But it seems it does not work. Here is the last one: select user.*, history.spent as intotal from user inner join (select username, sum(spent) as total from history group by username) history on user.username = history.username where user.locationid = 12
Asked by Imran Ahmed (1 rep)
May 28, 2023, 11:53 AM
Last activity: Jul 10, 2025, 06:02 PM