獲取 MySQL 中的列子字串
使用 SUBSTR() 方法獲取列的子字串。
我們首先建立一個表 -
mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.74 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable values('This is a MySQL Database');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Java is an Object Oriented Programming Language');
Query OK, 1 row affected (0.17 sec)使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable ;
輸出
這將生成以下輸出 -
+-------------------------------------------------+ | Title | +-------------------------------------------------+ | This is a MySQL Database | | Java is an Object Oriented Programming Language | +-------------------------------------------------+ 2 rows in set (0.00 sec)
以下是可以選擇列子字串的查詢 -
mysql> SELECT distinct(substr(Title, 1, 8)) from DemoTable;
輸出
這將生成以下輸出 -
+-----------------------+ | (substr(Title, 1, 8)) | +-----------------------+ | This is | | Java is | +-----------------------+ 2 rows in set (0.00 sec)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP