1

Teams table

+------+------------+
|team_id |    week    |
+------+------------+
|    1 | 2012-09-10 |
|    2 | 2012-09-10 |
|    3 | 2012-09-17 |
|    4 | 2012-09-17 |
+------+------------+

TeamPlayers Join Table

+---------+-----------+
| team_id | player_id |
+---------+-----------+
|       1 |         1 |
|       2 |         2 |
|       3 |         1 |
|       4 |         1 |
+---------+-----------+

Based on the schema above I need to put a query together that will show me which player was added and which player was dropped between this week and last week.

For example

Between week 2012-09-10 and 2012-1=09-17 Player 8 was moved on successfully (he appeared on one team last week and 2 teams this week).
Player 9 appeared on a team last week 2012-09-10 but did not make it to this week 2012-09-17.

I am currently doing this in the application code (because I am not a sql expert), but have a nagging feeling that there is likely a way to perform this directly in the database (mysql).

Any help would be greatly appreciated.

4

1 回答 1

0

这是一个开始,可能需要一些调整。希望能帮助到你。

SELECT * FROM teams t
INTO #TMP
INNER JOIN teamsplayers tp ON tp.team_id = t.team_id
;

SELECT count(*), week, player_id
FROM #TMP
GROUP BY week, player_id
ORDER BY week, player_id
;
Edit:

删除表#TMP;

于 2012-09-20T04:36:18.080 回答