为此,您可以使用SUBSTR()
方法。让我们首先创建一个表:
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20));
以下是使用insert命令在表中插入一些记录的查询:
mysql> insert into DemoTable(FirstName) values('John'); mysql> insert into DemoTable(FirstName) values('Carol'); mysql> insert into DemoTable(FirstName) values('Robert'); mysql> insert into DemoTable(FirstName) values('Chris'); mysql> insert into DemoTable(FirstName) values('David');
以下是使用select命令显示表中记录的查询:
mysql> select *from DemoTable;
这将产生以下输出:
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Carol | | 3 | Robert | | 4 | Chris | | 5 | David | +----+-----------+ 5 rows in set (0.00 sec)
以下是从每个字符串中获取最后三个字符的查询:
mysql> select substr(FirstName,-3) from DemoTable;
这将产生以下输出:
+----------------------+ | substr(FirstName,-3) | +----------------------+ | ohn | | rol | | ert | | ris | | vid | +----------------------+ 5 rows in set (0.00 sec)