gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:41:10 24 4
gpt4 key购买 nike

我的 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

关于MySQL子查询没有给我MAX或ORDER BY LIMIT 1的最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449678/

24 4 0
文章推荐: mysql - 在 Rails 中动态创建 mysql 列
文章推荐: javascript - 通过选择日期选择器创建数据表标题
文章推荐: javascript - 多个 .on ('click' ... 函数仅绑定(bind)到第一个
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com