如何在 MySQL 中從一個字串中從某個特定位置開始提取子串?


為此,你可以使用 mid() 函式。如下所示語法 −

select mid(yourColumnName, yourPositionToStart, yourEndValue) as anyAliasName from yourTableName;

首先,我們建立一個表 −

mysql> create table DemoTable
   -> (
   -> Title text
   -> );
Query OK, 0 rows affected (0.64 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable values('My best framework is Spring and Hibernate');
Query OK, 1 row affected (0.21 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| My best framework is Spring and Hibernate |
+-------------------------------------------+
1 row in set (0.00 sec)

下面是在 MySQL 中獲取字串一部分的查詢。我們嘗試從第 21 個字元開始獲取 30 個字元。如果 21 個字元之後的字元數不是 30,那麼只有剩下的字元將被顯示 −

mysql> select mid(Title,21,30) as partofString from DemoTable;

輸出

+-----------------------+
| partofString          |
+-----------------------+
| Spring and Hibernate  |
+-----------------------+
1 row in set (0.00 sec)

更新於: 30-7-2019

209 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.