更新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)
热门推荐
1 如何训练狗狗握手
10 新员工寄语祝福语简短
11 新婚敬酒祝福语大全简短
12 怎么训练狗狗
13 训练狗狗上厕所
14 春蚕老师祝福语简短励志
15 高考好运如愿祝福语简短
16 战哥过年祝福语简短
17 中考感谢朋友祝福语简短
18 长沙旅游祝福语大全简短