丢弃MySQL中字段的最后3个字符

让我们首先创建一个表-

create table DemoTable
   -> (
   -> StudentId varchar(100)
   -> );

使用插入命令在表中插入一些记录-

insert into DemoTable values('STU-090');

insert into DemoTable values('STU-123');

insert into DemoTable values('STU-678');

使用select语句显示表中的所有记录-

select *from DemoTable;

输出结果

这将产生以下输出-

+-----------+
| StudentId |
+-----------+
| STU-090   |
| STU-123   |
| STU-678   |
+-----------+
3 rows in set (0.00 sec)

以下是丢弃字段的后3个字符的查询-

SELECT REVERSE(SUBSTR(REVERSE(StudentId), 4)) from DemoTable

输出结果

这将产生以下输出-

+----------------------------------------+
| REVERSE(SUBSTR(REVERSE(StudentId), 4)) |
+----------------------------------------+
| STU-                                   |
| STU-                                   |
| STU-                                   |
+----------------------------------------+
3 rows in set (0.00 sec)