Created
September 4, 2013 18:05
-
-
Save psykidellic/6440552 to your computer and use it in GitHub Desktop.
Better way to keep user history?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I have a table where I keep record whenever somebody plays a track: | |
| id, user_id, song_id, created_at | |
| I was hoping to generate user history based on this so I have a query like: | |
| ``` | |
| select albums.id from albums join songs on albums.id = songs.album_id join | |
| song_played_stats on song_played_stats.song_id = songs.id order by | |
| song_played_stats.created_at desc; | |
| ``` | |
| 1 | |
| 418 | |
| 418 | |
| 5 | |
| 5 | |
| 5 | |
| 2 | |
| 2 | |
| 2 | |
| ... | |
| Improving it like: | |
| ``` | |
| select distinct(album_id) from albums join songs on albums.id = songs.album_id join song_played_stats on song_played_stats.song_id = songs.id order by song_played_stats.created_at desc; | |
| ``` | |
| reults in: | |
| 418 | |
| 80 | |
| 8 | |
| 5 | |
| 312 | |
| 81 | |
| .. | |
| As you can see, I would ideally like it to be: 1, 418, 5, 2.... | |
| Thus I am wondering if my way of doing things is incorrect. The other approach I thought was to add application logic and keep a schema like user_history where I only enter an album record if the last one was not same and get sorted result back. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment