更新字段是最小数字的位置

Update Where field is the lowest number

我想我是从错误的角度来看这个问题的,但我就是想不出一种方法来完成这项工作。我的查询如下:

UPDATE my_table AS T0 
JOIN my_table_2 AS T1
ON T0.timestamp = T1.timestamp  AND T0.id = T1.id

SET lat = lat + 1

WHERE T0.duration = 'd'
AND T0.unique >= 0 
AND T0.unique <= 5 

我需要更新 T0.unique 等于 0 到 5 之间的最小数字的位置。所以基本上如果上面的 returns 3 行,因为 T0.unique = 2 在一行中,并且 T0.unique = 3 在另一行,T0.unique = 4 在另一行,那么我只希望它实际更新它的最低数字,即 T0.unique = 2

我希望有一些我不知道的 MySQL 语法可以做到这一点。

你能不能不做这样的事情:

UPDATE mytable AS T0
SET lat = lat + 1
WHERE T0.duration = 'd' AND T0.unique BETWEEN 0 AND 5
--- possible alternative to join, allows use of ORDER BY and LIMIT in statement
AND EXISTS ( SELECT T1.id FROM mytable2 AS T1 
    WHERE T0.timestamp = T1.timestamp AND T0.id = T1.id )
ORDER BY T0.unique ASC
LIMIT 1

这将最多更新 1 条记录,并且 ORDER BY 确保它是 unique 列中具有最低值的记录。

你能试试这个吗:

UPDATE my_table t0 
  JOIN my_table_2 t1 
    ON t0.timestamp = t1.timestamp
   AND t0.id = t1.id
   SET t0.lat = t0.lat + 1 
 WHERE t0.duration = 'd'
   AND t0.unique BETWEEN 0 AND 5