您可以将update命令与用户定义的变量一起使用。 让我们首先创建一个表-
mysql> create table DemoTable ( FirstName varchar(20), Position int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris',100); mysql> insert into DemoTable values('Robert',120); mysql> insert into DemoTable values('David',130);
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+----------+ | FirstName | Position | +-----------+----------+ | Chris | 100 | | Robert | 120 | | David | 130 | +-----------+----------+ 3 rows in set (0.00 sec)
以下是对字段进行排序并将值增加10的查询-
mysql> SET @myPosition := 100; mysql> update DemoTable set Position = @myPosition:=@myPosition+10 ORDER BY Position; Rows matched: 3 Changed: 1 Warnings: 0
让我们再次从表中检查记录:mysql> select * from DemoTable-
这将产生以下输出-
+-----------+----------+ | FirstName | Position | +-----------+----------+ | Chris | 110 | | Robert | 120 | | David | 130 | +-----------+----------+ 3 rows in set (0.00 sec)