更新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 祝福语情人节简短