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)
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短