In the id_team field, display the name from the teams table. It is necessary to connect two fields from one table with one field from another. The fact is that there is a field id_team_one and id_team_two.
select *, win * 3 as win_ace, draw * 1 as draw_ace from( SELECT team_id, id_tournament, name_team_one, name_team_two, sum(win) AS win, sum(loss) AS loss, sum(draw) AS draw, sum(goals_s) AS goals_s, sum(goals_m) AS goals_m FROM (SELECT games.id_team_one as team_id, id_tournament, t1.name as name_team_one, IF(games.goals_one>games.goals_two,1,0) AS win, IF(games.goals_one<games.goals_two,1,0) AS loss, IF(games.goals_one=games.goals_two,1,0) AS draw, games.goals_one AS goals_s, games.goals_two AS goals_m FROM games WHERE games.`datetime` < NOW() UNION ALL SELECT games.id_team_two, id_tournament, t2.name as name_team_two, IF(games.goals_one<games.goals_two,1,0) AS win, IF(games.goals_one>games.goals_two,1,0) AS loss, IF(games.goals_one=games.goals_two,1,0) AS draw, games.goals_two AS goals_s, games.goals_one AS goals_m FROM games WHERE games.`datetime` < NOW() ) t join `tournaments` on id_tournament=tournaments.id join `teams` t1 on id_team_one=t1.id_team join `teams` t2 on id_team_two=t2.id_team GROUP BY team_id ) as foo order by ((win * 3) + (draw * 1)) desc Added join to teams table. Gives an error message. # 1054 - Unknown column 't1.name' in 'field list'
'ONE' AS team_nameand'TWO' AS team_nameto requests and consider it in the grouping - Chubatiy