Get the substring of a column in MySQL
mysqlmysqli database更新于 2025/5/26 5:32:17
Use the SUBSTR() method to get the substring of a column.
首先我们创建一个表 −
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

