获取子字符串(只从字符串中获取最后三个字符)的 MySQL 查询?
为此,你可以使用 SUBSTR() 方法。我们先创建一个表格
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20)); Query OK, 0 rows affected (1.31 sec)
下面是使用 insert 命令在表格中插入一些记录的查询
mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.17 sec)
下面是使用 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)
广告