5

我有这个 sql 查询,这是错误的。我想更新wins表中的字段“prevmonth”,但只更新字段“month_wins”的最大值的记录。

UPDATE wins 
SET prevmonth_top=1 
WHERE month_wins = (SELECT MAX(month_wins) FROM wins)

但是我该怎么做呢?

4

2 回答 2

19

试试这个技巧,

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您不能在 FROM 子句中指定目标表 'table_name' 进行更新

于 2012-09-03T05:39:39.660 回答
5

运行以下查询,它可能会有所帮助:

UPDATE wins 
SET prevmonth_top=1  WHERE month_wins = 
(
      SELECT month_wins FROM (SELECT MAX(month_wins) FROM wins) AS month_wins
) 
于 2012-09-03T05:43:27.703 回答