簡體   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