32
loading...
This website collects cookies to deliver better user experience
game
table of computer games, and a separate join table that I've called game_favourite
. The sole purpose of game_favourite
is to log which users have favourited which games. game_favourite
is unique on both game_id
and user_id
i.e. a user can only favourite a game once and there are no duplicate rows.game
table, and then for each row, going to the game_favourite
table to check whether a specific user (whose user_id
I passed into the function) had favourited that game. So the N
in N+1
comes from the number of rows in my game table (in this case 4), and the 1
comes from just doing the base query to return the game table as a whole in the first place.user_id
. So, here's what the tables look like and the desired output I want:// Starting point for **game** table
| id | name | favourite_count |
| --- | ------------------ | --------------- |
| 1 | ratchet and clank | 2 |
| 2 | assassins creed | 1 |
| 3 | far cry 5 | 1 |
| 4 | horizon: zero dawn | 0 |
// Starting point for **game_favourite** table
| id | game_id | user_id |
| --- | ------- | ------- |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 3 | 3 |
// Desired output for **user_id = 1** using the **game** table as a base
| name | favourite_count | user_has_favourited |
| ------------------ | --------------- | ------------------- |
| ratchet and clank | 2 | true |
| assassins creed | 1 | true |
| far cry 5 | 1 | null |
| horizon: zero dawn | 0 | null |
SELECT game.name, game.favourite_count, favourites.user_has_favourited
FROM game as game
LEFT OUTER JOIN (
SELECT game_id, user_id,
(CASE
WHEN user_id = '1' THEN TRUE
ELSE FALSE
END) AS user_has_favourited
FROM game_favourite
) AS favourites
ON game.id = favourites.game_id AND user_has_favourited = TRUE;
SELECT game.name, game.favourite_count, favourites.user_id IS NOT NULL as user_has_favourited
FROM game AS game
LEFT JOIN game_favourite AS favourites
ON game.id = favourites.game_id AND favourites.user_id = '1';
name
and favourite_count
columns.
SELECT game.name, game.favourite_count
FROM game AS game
user_id
data from the game_favourite
table, so we need to do a LEFT OUTER JOIN. This is the default LEFT JOIN in SQL, so you can omit the OUTER keyword. The joins criteria (i.e. the common field) is the game_id
.
SELECT game.name, game.favourite_count
FROM game AS game
LEFT JOIN game_favourite AS favourites
ON game.id = favourites.game_id
| name | favourite_count |
| ------------------ | --------------- |
| ratchet and clank | 2 |
| assassins creed | 1 |
| far cry 5 | 1 |
| horizon: zero dawn | 0 |
| ratchet and clank | 2 |
user_id
that we're interested in, in this case, user_id = 1
.
SELECT game.name, game.favourite_count
FROM game AS game
LEFT JOIN game_favourite AS favourites
ON game.id = favourites.game_id AND favourites.user_id = '1';
user_has_favourited
, which shows true
if user 1 has favourited the game.
SELECT game.name, game.favourite_count, favourites.user_id IS NOT NULL as user_has_favourited
FROM game AS game
LEFT JOIN game_favourite AS favourites
ON game.id = favourites.game_id AND favourites.user_id = '1';