如何从MySQL中的最后一个开始修剪X个字符?
为此,您可以substring()
与length()
一起使用。让我们首先创建一个表-
mysql> create table DemoTable1329 -> ( -> StudentName varchar(40) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1329 values('David Miller'); mysql> insert into DemoTable1329 values('Chris Brown'); mysql> insert into DemoTable1329 values('Adam Smith'); mysql> insert into DemoTable1329 values('John Doe');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1329;
这将产生以下输出-
+--------------+ | StudentName | +--------------+ | David Miller | | Chris Brown | | Adam Smith | | John Doe | +--------------+ 4 rows in set (0.00 sec)
这是从末尾开始修剪X个字符的查询-
mysql> select substring(StudentName, 1, length(StudentName) - 5) from DemoTable1329;
这将产生以下输出-
+----------------------------------------------------+ | substring(StudentName, 1, length(StudentName) - 5) | +----------------------------------------------------+ | David M | | Chris | | Adam | | Joh | +----------------------------------------------------+ 4 rows in set (0.00 sec)