如何限制 MySQL SELECT 結果中 longtext 欄位的長度?


可以使用 MySQL 中的 SUBSTRING() 來限制字串的長度。語法如下

SELECT SUBSTRING(yourColumnName,1,yourIntegerValueToGetTheCharacters) as
anyVariableName from yourTableName;

為了理解上述語法,讓我們建立一個表。建立表的查詢如下

mysql> create table limitLengthOfLongTextDemo
   -> (
   -> sentence LONGTEXT
   -> );
Query OK, 0 rows affected (0.74 sec)

使用插入命令插入一些記錄到表中。查詢如下

mysql> insert into limitLengthOfLongTextDemo values('This is the introduction to MySQL');
Query OK, 1 row affected (0.17 sec)

mysql> insert into limitLengthOfLongTextDemo values('PL/SQL is the extension of Structured
Query Language');
Query OK, 1 row affected (0.19 sec)

mysql> insert into limitLengthOfLongTextDemo values('Java is an Object Oriented
Programming Language');
Query OK, 1 row affected (0.20 sec)

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

mysql> select *from limitLengthOfLongTextDemo;

以下是輸出

+------------------------------------------------------+
| sentence                                             |
+------------------------------------------------------+
| This is the introduction to MySQL                    |
| PL/SQL is the extension of Structured Query Language |
| Java is an Object Oriented Programming Language      |
+------------------------------------------------------+
3 rows in set (0.00 sec)

以下是獲取給定值字元的查詢

mysql> select substring(sentence,1,26) as 26Characters from limitLengthOfLongTextDemo;

以下是輸出

+----------------------------+
| 26Characters               |
+----------------------------+
| This is the introduction t |
| PL/SQL is the extension of |
| Java is an Object Oriented |
+----------------------------+
3 rows in set (0.00 sec)

更新於: 30-Jul-2019

1K+ 瀏覽量

啟動您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.