要删除最后一个字符(如果是特定字符),请使用SUBSTRING()
。让我们首先创建一个表-
mysql> create table DemoTable ( SubjectName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('MySQL'); mysql> insert into DemoTable values('MongoDB?'); mysql> insert into DemoTable values('Java?'); mysql> insert into DemoTable values('C');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+-------------+ | SubjectName | +-------------+ | MySQL | | MongoDB? | | Java? | | C | +-------------+ 4 rows in set (0.00 sec)
这是删除最后一个字符(如果是特定字符)的查询,例如此处的问号(?)-
mysql> update DemoTable SET SubjectName = SUBSTRING(SubjectName, 1, CHAR_LENGTH(SubjectName) - 1) where SubjectName like '%?'; Rows matched : 2 Changed : 2 Warnings : 0
让我们再次检查表记录-
mysql> select *from DemoTable;
输出结果
+-------------+ | SubjectName | +-------------+ | MySQL | | MongoDB | | Java | | C | +-------------+ 4 rows in set (0.00 sec)