我可以结合使用这两个选择查询吗?

时间:2019-10-23 08:15:42

标签: mysql mysql-8.0

我正在使用两个查询。

首先从特定的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 :(得分:0)

考虑:

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 = ?
)

这将为您提供?所代表的用户参加的所有锦标赛和玩家的所有可用信息。