Могу ли я объединить эти два запроса? - PullRequest
0 голосов
/ 23 октября 2019

Я использую два запроса.

Сначала получим tournament_id от определенного userId. Второй запрос дает мне информацию о самом лечении и других игроках в турнире и их информацию.

  #Check if `userId` exists in tournaments if found save the value into a variable.

        SELECT 
            tournament_id
        INTO @tournament_id FROM
            tournament_players
        WHERE
            userId = 5324234;


    #Get information about the tournament itself and which (if any) 
     additional users with there info.

        SELECT 
            *
        FROM
            tournaments AS tour
                JOIN
            tournament_players AS tp ON tour.id = tp.tournament_id
                AND tour.id = @tournament_id;

1 Ответ

0 голосов
/ 23 октября 2019

Рассмотрим:

select t.*, tp.*
from trivia_tournaments.tournaments t
inner join tournament_players tp on tp.tournament_id = t.id
where exists (
    select 1
    from tournament_players tp1 
    where tp1.tournament_id = t.id
    and tp1.user_id = ?
)

Это даст вам всю доступную информацию обо всех турнирах и игроках, в которых участвовал пользователь, обозначенный ?.

...