只返回 MySQL 中字串值列的前 15 個字元


要只返回字串值的前 15 個字元,請使用 MySQL SUBSTR() 函式。

我們首先建立一個表 -

mysql> create table DemoTable
(
   Title varchar(100)
);
Query OK, 0 rows affected (0.69 sec)

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

mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Introduction to Java');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('C in Depth with data structure and algorithm');
Query OK, 1 row affected (0.15 sec)

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

mysql> select *from DemoTable;

這將生成以下輸出 -

+----------------------------------------------+
| Title                                        |
+----------------------------------------------+
| Introduction to MySQL                        |
| Introduction to Java                         |
| C in Depth with data structure and algorithm |
+----------------------------------------------+
3 rows in set (0.00 sec)

現在,讓我們實現查詢以僅獲取前 15 個字元 -

mysql> select substr(Title,1,15) from DemoTable;

這將生成以下輸出 -

+--------------------+
| substr(Title,1,15) |
+--------------------+
| Introduction to    |
| Introduction to    |
| C in Depth with    |
+--------------------+
3 rows in set (0.00 sec)

更新於: 26-Sep-2019

597 次瀏覽

開啟你的職業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.