在 MySQL 中获取最后一个点后的子字符串

mysqlmysqli database更新于 2024/3/15 8:01:00

要获取最后一个点后的子字符串,请使用 substring_index()。让我们首先创建一个表−

mysql> create table DemoTable1341
   -> (
   -> Value varchar(60)
   -> );
Query OK, 0 rows affected (0.75 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable1341 values('John.123.@gmail.com' );
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable1341 values('Carol.Taylor.gmail') ;
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable1341 values('C.MyFolder.Location') ;
Query OK, 1 row affected (0.10 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1341;

这将产生以下输出 −

+---------------------+
| Value               |
+---------------------+
| John.123.@gmail.com |
| Carol.Taylor.gmail  |
| C.MyFolder.Location |
+---------------------+
3 rows in set (0.00 sec)

这是获取最后一个点后的子字符串的查询 −

mysql> select substring_index(Value, '.', -1) from DemoTable1341;

这将产生以下输出 −

+---------------------------------+
| substring_index(Value, '.', -1) |
+---------------------------------+
| com                             |
| gmail                           |
| Location                        |
+---------------------------------+
3 rows in set (0.00 sec)

相关文章