We have an app that helps people find other people to play basketball. We are associated with some basketball courses in the city. So I have been tasked to find 'how many times players who play at XXField (field_id = 460), play basketball the whole year'
I am not sure I understand what to do very well. These are the tables I am dealing with
Game_users Table
game_id | user_id | bill_id | created_at | deleted_at
23455 | 3455 | 234 | 2019-02-06 03:16:40 | null
45678 | 6790 | 877 | 2020-02-09 07:26:34 | null
87666 | 2546 | 899 | 2020-04-20 06:06:06 | null
12312 | 1231 | 989 | 2019-02-22 09:22:31 | null
Games Table (that includes the basketball field's id)
id | field_id | game_status| created_at | deleted_at
23455| 460 | Completed | 2019-02-06 03:16:40 | null
23455| 345 | Completed | 2020-09-03 05:45:33 | null
87666| 460 | Completed | 2020-12-12 08:34:22 | null
87666| 212 | Completed | 2020-11-23 09:54:12 | null
I know I have to join both tables to get the field id and the players that played there. But how do I also get the total times they played golf this year as well? including the ones they played at 460.
I am sorry this is confusing, it's very confusing to me too.
My expected results would be something like
User_Id | Total Games | FieldXX Number of Games
1234 | 34 | 12
You can use the sub-query in IN
clause and GROUP BY
as follows:
SELECT GU.USER_ID,
COUNT(1)
FROM GAME_USERS AS GU
WHERE GU.GAME_ID IN (SELECT ID FROM GAMES AS G WHERE G.FIELD_ID = 460)
AND .. CONDITION ON created_at FOR WHOLE YEAR ACCORDING TO YOUR REQUIREMENT
GROUP BY GU.USER_ID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.