要在MySQL中获取字符串的前n个字符,请使用LEFT()。为了获得字符串的最后n个字符,该RIGHT()方法在MySQL中使用。
RIGHT()method的语法如下-SELECT RIGHT(yourColumnName, valueOfN) as anyVariableName from yourTableName;
为了理解上述概念,让我们创建一个表。创建表的查询如下-mysql> create table gettingLast5Characters
−> (
−> BookName varchar(100)
−> );
现在,您可以使用insert命令在表中插入记录。查询如下-mysql> insert into gettingLast5Characters values('Introduction to C');
mysql> insert into gettingLast5Characters values('C in Depth');
mysql> insert into gettingLast5Characters values('Introduction to Java');
mysql> insert into gettingLast5Characters values('Let us C');
使用select语句显示表中的所有记录。显示所有记录的查询如下-mysql> select *from gettingLast5Characters;
以下是输出-+----------------------+
| BookName |
+----------------------+
| Introduction to C |
| C in Depth |
| Introduction to Java |
| Let us C |
+----------------------+
4 rows in set (0.00 sec)
这是获取字符串的最后5个字符的查询-mysql> select RIGHT(BookName,5) as Last5Character from gettingLast5Characters;
以下是输出-+----------------+
| Last5Character |
+----------------+
| to C |
| Depth |
| Java |
| us C |
+----------------+
4 rows in set (0.04 sec)
查看上面的示例输出,空间也在计数。