更新MySQL中的表格并在新列中仅显示缩写名称
要获得首字母缩写,请使用left()连同substring_index()的概念。
让我们创建一个表-
mysql> create table demo13 −> ( −> full_name varchar(100), −> short_name varchar(20) −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo13(full_name) values('John Smith');
mysql> insert into demo13(full_name) values('David Miller');
mysql> insert into demo13(full_name) values('Chris Brown');使用select语句显示表中的记录-
mysql> select *from demo13;
这将产生以下输出-
+--------------+------------+ | full_name | short_name | +--------------+------------+ | John Smith | NULL | | David Miller | NULL | | Chris Brown | NULL | +--------------+------------+ 3 rows in set (0.00 sec)
以下是更新表并获取缩写名称的查询-
mysql> update demo13 −> set short_name= concat( −> left(full_name, 1), −> left(substring_index(full_name, ' ', −1), 1) −> ); Rows matched: 3 Changed: 3 Warnings: 0
使用select语句显示表中的记录-
mysql> select *from demo13;
这将产生以下输出-
+--------------+------------+ | full_name | short_name | +--------------+------------+ | John Smith | JS | | David Miller | DM | | Chris Brown | CB | +--------------+------------+ 3 rows in set (0.00 sec)
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短