我給類似下面的嘗試,通過它假設一個主鍵在這張桌子上。如果沒有主鍵,您將很容易更新特定的行,否則會出現很多重複項。
所以在這個例子中,我會假設表如下
someTable (
pkID (Primary Key),
position,
Average,
gpm
)
所以下面的INSERT會做我希望
INSERT INTO someTable (
pkID,
position
)
SELECT
someTable.pkID,
calcTable.position
FROM someTable
INNER JOIN (
SELECT
MIN(c.position) AS position,
c.Average,
c.gpm
FROM (
// Calculate the position for each Average/gpm combination
SELECT
@p = @p + 1 AS position,
someTable.Average,
someTable.gpm
FROM (
SELECT @p:=0
) v,someTable
ORDER BY
someTable.Average DESC,
someTable.gpmp ASC
) c
// Now regroup to get 1 position for each combination (the lowest position)
GROUP BY c.Average,c.gpm
) AS calcTable
// And then join this calculated table back onto the original
ON (calcTable.Average,calcTable.gpm) = (someTable.Average,someTable.gpm)
// And rely on the PK IDs clashing to allow update
ON DUPLICATE KEY UPDATE position = VALUES(position)
自己試過什麼?如果是這樣,在這裏發佈 – xbonez 2012-04-19 14:51:58