丢弃 MySQL 字段中最后的 3 个字符
让我们首先创建一个表 -
mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable values('STU-090'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('STU-123'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('STU-678'); Query OK, 1 row affected (0.29 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
输出
这将生成以下输出 -
+-----------+ | StudentId | +-----------+ | STU-090 | | STU-123 | | STU-678 | +-----------+ 3 rows in set (0.00 sec)
以下是丢弃字段中最后 3 个字符的查询 -
mysql> SELECT REVERSE(SUBSTR(REVERSE(StudentId), 4)) from DemoTable
输出
这将生成以下输出 -
+----------------------------------------+ | REVERSE(SUBSTR(REVERSE(StudentId), 4)) | +----------------------------------------+ | STU- | | STU- | | STU- | +----------------------------------------+ 3 rows in set (0.00 sec)
广告