在 MySQL 中獲取最後一個點號之後的子字串


要獲取最後一個點號後的子字串,請使用 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)

更新日期:2019 年 11 月 5 日

1 千+ 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.