使用 MySQL 查询获取字符串的最后 5 个字符?

王林
王林 转载
2023-09-06 17:13:09 325浏览

使用 MySQL 查询获取字符串的最后 5 个字符?

要使用 MySQL 获取字符串的前 n 个字符,请使用 LEFT()。为了获取字符串的最后 n 个字符,MySQL 中使用 RIGHT() 方法。

RIGHT() 方法的语法如下 -

SELECT RIGHT(yourColumnName, valueOfN) as anyVariableName from yourTableName;

为了理解上述概念,让我们创建一个表。创建表的查询如下 -

mysql> create table gettingLast5Characters
   −> (
   −> BookName varchar(100)
   −> );
Query OK, 0 rows affected (0.73 sec)

现在您可以使用插入命令在表中插入记录。查询如下 -

mysql> insert into gettingLast5Characters values('Introduction to C');
Query OK, 1 row affected (0.19 sec)

mysql> insert into gettingLast5Characters values('C in Depth');
Query OK, 1 row affected (0.18 sec)

mysql> insert into gettingLast5Characters values('Introduction to Java');
Query OK, 1 row affected (0.18 sec)

mysql> insert into gettingLast5Characters values('Let us C');
Query OK, 1 row affected (0.51 sec)

使用 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)

看看上面的示例输出,空间也在计数。

以上就是使用 MySQL 查询获取字符串的最后 5 个字符?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除