繁体   English   中英

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

[英]MySQL subquery does not give me the highest value with MAX or ORDER BY LIMIT 1

我的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  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)
            ORDER BY last.id DESC
            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.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
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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