我有这个SQL查询,这是错误的。我想更新字段" prevmonth"在wins表中,但仅适用于具有该字段的最大值的记录" month_wins"。
UPDATE wins
SET prevmonth_top=1
WHERE month_wins = (SELECT MAX(month_wins) FROM wins)
但我怎么能这样做?
答案 0 :(得分:16)
尝试这个技巧,
UPDATE wins
SET prevmonth_top=1
ORDER BY month_wins DESC
LIMIT 1
或类似的东西,
UPDATE IGNORE giveaways
SET winner = 1
WHERE month_wins = (select maxID from (SELECT MAX(ID) maxID FROM giveaways) as t)
SAME AS You can't specify target table 'table_name' for update in FROM clause
答案 1 :(得分:5)
运行以下查询,它可能会有所帮助:
UPDATE wins
SET prevmonth_top=1 WHERE month_wins =
(
SELECT month_wins FROM (SELECT MAX(month_wins) FROM wins) AS month_wins
)