根据输出限制查询

时间:2013-09-13 11:15:27

标签: mysql

我正在编写运动情境的代码,其中查询返回谁承认最少的目标。

SELECT t.user AS userid, t.team AS teamid, dteam.name AS teamname, l.id AS leagueid, l.name AS leaguename, l.season AS leagueseason, l.career AS careerid
FROM tbl_foot_tables t
INNER JOIN tbl_foot_career_teams team ON t.team = team.id
INNER JOIN tbl_foot_career_db_teams dteam ON dteam.id = team.teamid
INNER JOIN tbl_foot_leagues l ON l.id = t.league
WHERE t.league = 263
ORDER BY t.home_goals_against + t.away_goals_against LIMIT 1

我限制为1,因为我希望失球最少的球员。但是,在2/3/4玩家都承认相同数量的目标的情况下,我需要它来返回所有这些行。

如果我不限制,我会返回所有行,即使承认的目标数量不同。

我不知道在这种情况下该怎么做,因为我以前从来没有碰过它。

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

您可以通过计算子查询中的最佳目标并使用join来获取值来实现此目的。然后选择与之匹配的所有团队:

SELECT t.user AS userid, t.team AS teamid, dteam.name AS teamname, l.id AS leagueid,
       l.name AS leaguename, l.season AS leagueseason, l.career AS careerid
FROM tbl_foot_tables t INNER JOIN
     tbl_foot_career_teams team
     ON t.team = team.id INNER JOIN
     tbl_foot_career_db_teams dteam
     ON dteam.id = team.teamid INNER JOIN
     tbl_foot_leagues l
     ON l.id = t.league join
     (select t.home_goals_against + t.away_goals_against as goals
      from tbl_foot_tables t
      WHERE t.league = 263
      order by  t.home_goals_against + t.away_goals_against desc
      limit 1
     ) as thebest
     on  t.home_goals_against + t.away_goals_against = thebest.goals
WHERE t.league = 263 ;