Skip to content
Advertisement

MySQL query to sum one column and count another column, from two tables, based on a common value?

I’m having issues getting my head around a query that I am trying to build. I’m hoping for a bit of help here…

I have two tables. Each table has two columns. They are described as follows:

Table: “Token”

Table: “Score”

I want a query that will list each name once, and with each name the sum of Points for that name, and count of where Color=”RED”

Sample data for table 1:

Sample data for table 2:

My Table output would be similar to (row order isn’t important):

How can I accomplish this?

Advertisement

Answer

You can aggregate, then join:

If names are missing in either table, then you can use union all instead:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement