您可以借助SUBSTRING()
和CHAR_LENGTH()方法去除最后两个字符。语法如下-
select yourColumnName,SUBSTRING(yourColumnName,1,CHAR_LENGTH(yourColumnName) - 2) AS anyVariableName from yourTableName;
要了解上述语法,让我们创建一个表-
mysql> create table LastTwoCharacters −> ( −> Words varchar(200) −> );
现在,您可以在select语句的帮助下在表中插入一些记录。插入记录的查询如下-
mysql> insert into LastTwoCharacters values('Hellooo'); mysql> insert into LastTwoCharacters values('Worldsss'); mysql> insert into LastTwoCharacters values('Johnson');
在select语句的帮助下显示所有记录-
mysql> select *from LastTwoCharacters;
以下是输出-
+----------+ | Words | +----------+ | Hellooo | | Worldsss | | Johnson | +----------+ 3 rows in set (0.00 sec)
以下是去除一列最后两个字符的查询-
mysql> select Words,SUBSTRING(Words,1,CHAR_LENGTH(Words) - 2) AS AfterStripLastTwoChar from LastTwoCharacters;
以下是输出-
+----------+-----------------------+ | Words | AfterStripLastTwoChar | +----------+-----------------------+ | Hellooo | Hello | | Worldsss | Worlds | | Johnson | Johns | +----------+-----------------------+ 3 rows in set (0.00 sec)