Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum values when joining tables?

Tags:

sql

mysql

<hyperbole>Whoever answers this question can claim credit for solving the world's most challenging SQL query, according to yours truly.</hyperbole>

Working with 3 tables: users, badges, awards.

Relationships: user has many awards; award belongs to user; badge has many awards; award belongs to badge. So badge_id and user_id are foreign keys in the awards table.

The business logic at work here is that every time a user wins a badge, he/she receives it as an award. A user can be awarded the same badge multiple times. Each badge is assigned a designated point value (point_value is a field in the badges table). For example, BadgeA can be worth 500 Points, BadgeB 1000 Points, and so on. As further example, let's say UserX won BadgeA 10 times and BadgeB 5 times. BadgeA being worth 500 Points, and BadgeB being worth 1000 Points, UserX has accumulated a total of 10,000 Points ((10 x 500) + (5 x 1000)).

The end game here is to return a list of top 50 users who have accumulated the most badge points.

Can you do it?

like image 244
keruilin Avatar asked Nov 24 '25 14:11

keruilin


1 Answers

My sample tables are:

user:

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(200) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

badge:

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| score | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

award:

+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id       | int(11) | YES  |     | NULL    |       |
| user_id  | int(11) | YES  |     | NULL    |       |
| badge_id | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+

Thus the query is:

SELECT user.name, SUM(score)
  FROM badge JOIN award ON badge.id = award.badge_id
       JOIN user ON user.id = award.user_id
 GROUP BY user.name
 ORDER BY 2
 LIMIT 50
like image 164
MoshiBin Avatar answered Nov 26 '25 07:11

MoshiBin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!