0

私はゲームを持っています、テーブルは次のようになります:

// 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;
}

しかし、1つのselectステートメントでこれを行う方法はありますか?

ありがとう

4

2 に答える 2

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'
于 2012-07-22T23:16:45.803 に答える
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'
于 2012-07-22T23:22:30.383 に答える