简体   繁体   中英

Setting a variable in mysql query

I am trying to display the head to head win loss records for two teams to display on the game results page. It's a cumulative tally that will change after each match up between competing teams. I need to define the 'Team' and 'Opponent' variables to run the query, but I cant seem to get it to work. The $GameID variable is passing via the GET method, but I need to set the Team and Opponent variable for my query to be the Team and Opponent for the particular GameID.

SELECT @team := Team, @opponent := Opponent FROM MatchDetails2017 WHERE GameID = $GameID AND HA = 'H')

                                            SELECT @Team, @Opponent,
                                            COUNT(*) AS Played,
                                            SUM(WL LIKE 'W%') AS Won,
                                            SUM(WL LIKE 'L%') AS Lost,
                                            SUM(WL = 'D') AS Drawn 
                                            FROM MatchDetails2017 
                                            WHERE Team = '@Team'
                                            AND Opponent = '@Opponent'
                                            AND GameID <= $GameID
                                            ");

Assigning a variable in MYSQL:

select @var1:= column1,@var2:=column2  from yourtable;
select @var1,@var2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM