要获得首字母缩写,请使用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)