Skip to content

Instantly share code, notes, and snippets.

@RobinStamer
Created July 7, 2011 02:00
Show Gist options
  • Select an option

  • Save RobinStamer/1068764 to your computer and use it in GitHub Desktop.

Select an option

Save RobinStamer/1068764 to your computer and use it in GitHub Desktop.
mysql> SELECT AVG(rating), songs.song_id FROM songs LEFT JOIN ratings USING (song_id) GROUP BY songs.song_id;
+-------------+---------+
| AVG(rating) | song_id |
+-------------+---------+
| NULL | 1 |
| 2.5000 | 2 |
| NULL | 3 |
+-------------+---------+
3 rows in set (0.02 sec)
mysql> SELECT * FROM songs;
+---------+
| song_id |
+---------+
| 1 |
| 2 |
| 3 |
+---------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM ratings;
+---------+--------+
| song_id | rating |
+---------+--------+
| 2 | 4 |
| 2 | 2 |
| 2 | 3 |
| 2 | 1 |
+---------+--------+
4 rows in set (0.00 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment