Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL子查询没有给我MAX或ORDER BY LIMIT 1的最大值_Mysql_Sql_Database_Subquery - Fatal编程技术网

MySQL子查询没有给我MAX或ORDER BY LIMIT 1的最大值

MySQL子查询没有给我MAX或ORDER BY LIMIT 1的最大值,mysql,sql,database,subquery,Mysql,Sql,Database,Subquery,我的MySQL查询有问题。其子查询未给出last.id的最高值 我也尝试过这种方法,但不起作用: SELECT rounds.winners, rounds.losers FROM players INNER JOIN teams ON teams.id = players.ilmo_id INNER JOIN status AS first ON first.id = players.status_id LEFT JOIN matches ON matches.chart_id = 12

我的MySQL查询有问题。其子查询未给出last.id的最高值

我也尝试过这种方法,但不起作用:

SELECT rounds.winners, rounds.losers
FROM players
INNER JOIN teams ON teams.id = players.ilmo_id
INNER JOIN status AS first ON first.id = players.status_id
LEFT JOIN matches ON matches.chart_id = 12
  AND matches.id = (
    SELECT  MAX(last.id)
    FROM    matches AS last
    WHERE   (last.player1_id = players.id
            OR last.player2_id = players.id
            OR last.player3_id = players.id
            OR last.player4_id = players.id)
        )

 JOIN charts ON charts.id = matches.chart_id
 JOIN places ON charts.template_id = places.template_id AND places.id = matches.place_id
 JOIN templates ON places.template_id = templates.id
 JOIN rounds ON places.round_id = rounds.id
 WHERE players.comp_id = 12
编辑:

这是最新的版本,看起来还可以

SELECT rounds.winners, rounds.losers
FROM players
INNER JOIN teams ON teams.id = players.ilmo_id
INNER JOIN status AS first ON first.id = players.status_id
LEFT JOIN matches ON matches.chart_id = 12
  AND matches.id = (
    SELECT  last.id
    FROM    matches AS last
    WHERE   last.chart_id = 12 /* modified */
            AND (last.player1_id = players.id
            OR last.player2_id = players.id
            OR last.player3_id = players.id
            OR last.player4_id = players.id)
            ORDER BY last.place_id DESC /* modified */
            LIMIT 1
        )

 JOIN charts ON charts.id = matches.chart_id
 JOIN places ON charts.template_id = places.template_id AND places.id = matches.place_id
 JOIN templates ON places.template_id = templates.id
 JOIN rounds ON places.round_id = rounds.id
 WHERE players.comp_id = 12

几乎可以肯定的是,以前有过数千次的提问和回答,但如果仍在挣扎,看看你加入比赛的目的是什么?on子句看起来更像where子句的条件。您正在加入chart_id=12且是最高id的匹配,但在子查询中,您没有引用chart_id。它返回所有chart_id中的最高id。因此,如果最高ID行没有chart\u ID=12,on子句将不匹配。@现在我的子查询有WHERE last.chart\u ID=12和last.player1\u ID。。。好吗?是的,我现在明白了。我认为您不需要匹配。打开后立即使用chart_id=12。这是多余的,因为它是在子查询中处理的。您认为maxlast.id比使用order by和limit 1更好吗?只是因为它是跨平台的标准。但我会使用提供最佳性能的语法。
SELECT rounds.winners, rounds.losers
FROM players
INNER JOIN teams ON teams.id = players.ilmo_id
INNER JOIN status AS first ON first.id = players.status_id
LEFT JOIN matches ON matches.chart_id = 12
  AND matches.id = (
    SELECT  last.id
    FROM    matches AS last
    WHERE   last.chart_id = 12 /* modified */
            AND (last.player1_id = players.id
            OR last.player2_id = players.id
            OR last.player3_id = players.id
            OR last.player4_id = players.id)
            ORDER BY last.place_id DESC /* modified */
            LIMIT 1
        )

 JOIN charts ON charts.id = matches.chart_id
 JOIN places ON charts.template_id = places.template_id AND places.id = matches.place_id
 JOIN templates ON places.template_id = templates.id
 JOIN rounds ON places.round_id = rounds.id
 WHERE players.comp_id = 12
SELECT rounds.winners, rounds.losers
FROM players
INNER JOIN teams ON teams.id = players.ilmo_id
INNER JOIN status AS first ON first.id = players.status_id
LEFT JOIN matches ON matches.id = (
    SELECT  max(last.id)
    FROM    matches AS last
    WHERE   last.chart_id = 12 /* modified */
            AND (last.player1_id = players.id
            OR last.player2_id = players.id
            OR last.player3_id = players.id
            OR last.player4_id = players.id)
        )
 JOIN charts ON charts.id = matches.chart_id
 JOIN places ON charts.template_id = places.template_id AND places.id = matches.place_id
 JOIN templates ON places.template_id = templates.id
 JOIN rounds ON places.round_id = rounds.id
 WHERE players.comp_id = 12