如何從 MySQL 列中獲取前 N 個字元


使用 SUBSTRING() 從 MySQL 列獲取前 N 個字元。我們首先建立一個表 -

mysql>create table DemoTable
(
   Information text
);
Query OK, 0 rows affected (2.63 sec)

使用插入命令在表中插入記錄 -

mysql>insert into DemoTable values('MySQL is a structured query language');
Query OK, 1 row affected (0.13 sec)

以下是使用 select 語句從表中顯示所有記錄的查詢 -

mysql>select *from DemoTable;

這會產生以下輸出 -

+--------------------------------------+
| Information                          |
+--------------------------------------+
| MySQL is a structured query language |
+--------------------------------------+
1 row in set (0.00 sec)

這是從列中獲取前 N 個字元的查詢。在本例中,我們選擇了前 10 個字元 -

mysql>select substring(Information,1,10) from DemoTable;

這會產生以下輸出 -

+-----------------------------+
| substring(Information,1,10) |
+-----------------------------+
| MySQL is a                  |
+-----------------------------+
1 row in set (0.00 sec)

更新於: 2019 年 7 月 30 日

859 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.