如何從 MySQL 文字欄位中檢索前 40 個字元?


要從文字欄位獲取前 40 個字元,請使用 MySQL 中的 LEFT() 函式。語法如下 −

SELECT LEFT(yourColumnName,40) as anyVariableName from yourTableName;

為了瞭解上述概念,讓我們建立一個表。建立表的查詢如下 −

mysql> create table retrieveFirst40Characters
   −> (
   −> AllWords text
   −> );
Query OK, 0 rows affected (0.59 sec)

現在,你可以藉助插入命令將一些記錄插入到表中。查詢如下 −

mysql> insert into retrieveFirst40Characters values('This is a query demo to extract a forty characters from a text
'> field,you can use left function to get forty characters');
Query OK, 1 row affected (0.32 sec)

使用 select 語句顯示錶中的所有記錄。查詢如下 −

mysql> select *from retrieveFirst40Characters;

以下是輸出 −

+------------------------------------------------------------------------------------------------------------------------+
| AllWords                                                                                                               |
+------------------------------------------------------------------------------------------------------------------------+
| This is a demo text displayed in a table for our example                                                               |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

以下是如何從文字欄位中提取前 40 個字元的查詢 −

mysql> select left(AllWords,40) as Retrieve40Characters from retrieveFirst40Characters;

以下是顯示前 40 個字元的輸出 −

+------------------------------------------+
| Retrieve40Characters                     |
+------------------------------------------+
| This is a demo text displayed in a table |
+------------------------------------------+
1 row in set (0.03 sec)

更新於: 2019 年 7 月 30 日

127 次瀏覽

開啟 職業生涯

完成課程可獲得認證

開始
廣告
© . All rights reserved.