我有一个游戏,表格如下:
// users
user_id | favorite_color
// games
game_id | game_name
// game_participants
id | fk_game_id | fk_user_id
我想获得特定游戏中所有用户最喜欢的颜色。我可以通过几个步骤完成此操作,例如:
// get the game.
Game game = select * from games where game_id = 'abc';
// get each user's favorite color, one at a time.
for (participants in game) {
select favorite_color from users where user_id = game.participants[i].id;
}
但是有没有办法在一个select语句中执行此操作?
由于
答案 0 :(得分:1)
SELECT favorite_color
FROM games
INNER JOIN game_participants on games.game_id = game_participants.fk_game_id
INNER JOIN users on users.user_id = game_participants.fk_user_id
WHERE game_id = 'abc'
答案 1 :(得分:1)
SELECT users.favourite_color
FROM game_participants
INNER JOIN users ON game_participants.fk_user_id = users.user_id
WHERE fk_game_id = 'abc'