为此,请使用LEFT()
方法的概念。让我们首先创建一个表-
mysql> create table DemoTable2036 -> ( -> FirstLetter varchar(20), -> Title varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable2036(Title) values('Chris'); mysql> insert into DemoTable2036(Title) values('John'); mysql> insert into DemoTable2036(Title) values('Adam');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable2036;
这将产生以下输出-
+-------------+-------+ | FirstLetter | Title | +-------------+-------+ | NULL | Chris | | NULL | John | | NULL | Adam | +-------------+-------+ 3 rows in set (0.00 sec)
以下是获取列值的第一个字母并将其放置到另一列的查询-
mysql> update DemoTable2036 -> set FirstLetter=left(Title,1); Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable2036;
这将产生以下输出-
+-------------+-------+ | FirstLetter | Title | +-------------+-------+ | C | Chris | | J | John | | A | Adam | +-------------+-------+ 3 rows in set (0.00 sec)