Вы можете попробовать подсчитать предметы в еде.
db.collection.aggregate([{$project: {numberOfFoos: { $size: "$foos" } } } ] )
Слева присоединиться к пользователям дважды и включить исключение ID 2 в предложении ON
. Получите изображение профиля, которое не NULL
, используя coalesce()
.
SELECT g.games_id,
coalesce(u1.profile_pic,
u2.profile_pic) profile_pic
FROM games g
LEFT JOIN users u1
ON u1.users_id = g.player_1_users_id
AND g.player_1_users_id <> 2
LEFT JOIN users u2
ON u2.users_id = g.player_2_users_id
AND g.player_2_users_id <> 2
WHERE 2 IN (g.player_1_users_id,
g.player_2_users_id)
AND g.status = 3;