更新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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短