MySQL查询使用数值增量值(如John1,John2,John3等)更新列中的所有值。
将列中的所有值更新为John1,John2等;您需要设置增量值1、2、3等,并将它们连接到记录。让我们首先创建一个表-
mysql> create table DemoTable ( StudentId varchar(80) );
使用insert命令在表中插入一些记录。在这里,对于我们的示例,我们设置了相似的名称-
mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values('John');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+ | StudentId | +-----------+ | John | | John | | John | | John | | John | +-----------+ 5 rows in set (0.00 sec)
以下是使用数字增量值更新/连接所有名称的查询-
mysql> update DemoTable,(select @row := 0) r set StudentId =concat('John',@row := @row+ 1); Rows matched: 5 Changed: 5 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+ | StudentId | +-----------+ | John1 | | John2 | | John3 | | John4 | | John5 | +-----------+ 5 rows in set (0.00 sec)