0

有没有办法在 MySQL 更新查询中计算 Int?

目前我有

UPDATE mails SET uid = 4275

我想要类似的东西

UPDATE mails SET uid = (4275++)
4

6 回答 6

3

你想做这样的事情吗?

SELECT @i:=425;

UPDATE mails SET uid = @i:=@i+1;
于 2012-10-08T08:37:23.117 回答
1

如果您需要更新表以使每个表增加uid1,那么您可以这样做:

   UPDATE mails 
   SET uid = uid + 1;

但是,如果您需要将每个值uid增加一个增量值,请尝试以下操作:

SET @counter = 0; 
UPDATE mails m1
INNER JOIN
(
    SELECT *, (@counter := @counter +1) as counter
    FROM mails
) m2 ON m1.uid = m2.uid
SET m1.uid = m1.uid + m2.counter

如果你想从 4275 开始计数,只需将计数器设置为SET @counter = 4275

于 2012-10-08T08:32:05.897 回答
1

更新邮件 SET uid = uid + x

x 表示任何数字

我没有正确回答您的问题,但我只是按照我的理解回答

更新邮件 SET uid = uid + 1

即通过lastuid + 1增加当前uid

于 2012-10-08T08:34:38.723 回答
0

UPDATE mails SET uid = uid +1将为每个 uid 列添加 1。

于 2012-10-08T08:35:45.397 回答
0

尝试

UPDATE mails SET uid = uid+1
where uid = 4275
于 2012-10-08T08:35:49.390 回答
-1

您可以保留自己的柜台

declare @curr_uid integer; 
SET @curr_uid = 4275; -- initial uid

update mails
set uid = @curr_uid , @curr_uid = @curr_uid + 1
于 2012-10-08T08:38:32.400 回答