用於從欄位中提取最後單詞的 MySQL 查詢?
要從欄位中提取最後一個單詞,請使用內建的 SUBSTRING_INDEX() 函式。語法如下 −
SELECT SUBSTRING_INDEX(yourColumnName,’ ‘,-1) as anyVariableName from yourTableName;
為了理解上述概念,讓我們建立一個表。以下是用於建立表的查詢 −
mysql> create table FirstWordDemo −> ( −> AllWords longtext −> ); Query OK, 0 rows affected (0.83 sec)
現在,使用插入命令在表中插入一些單詞。查詢如下 −
mysql> insert into FirstWordDemo values('This is the first MySQL Query');
Query OK, 1 row affected (0.11 sec)
mysql> insert into FirstWordDemo values('MySQL is a Relational Database');
Query OK, 1 row affected (0.17 sec)
mysql> insert into FirstWordDemo values('FirstWord is not correct');
Query OK, 1 row affected (0.21 sec)現在,使用選擇語句顯示錶中的所有記錄。查詢如下 −
mysql> select *from FirstWordDemo;
以下是輸出 −
+--------------------------------+ | AllWords | +--------------------------------+ | This is the first MySQL Query | | MySQL is a Relational Database | | FirstWord is not correct | +--------------------------------+ 3 rows in set (0.00 sec)
以下是顯示欄位中最後一個單詞的查詢。我們在開始時討論了相同的語法。以下是查詢 −
mysql> select SUBSTRING_INDEX(AllWords, ' ', -1) as MyFirstWordResult from FirstWordDemo;
以下是輸出 −
+-------------------+ | MyFirstWordResult | +-------------------+ | Query | | Database | | correct | +-------------------+ 3 rows in set (0.00 sec)
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP