在 MySQL 中仅返回包含字符串值的列的前 15 个字符
mysqlmysqli database更新于 2025/12/15 15:40:17
要仅返回字符串值的前 15 个字符,请使用 MySQL SUBSTR() 函数。
首先我们创建一个表 −
mysql> create table DemoTable ( Title varchar(100) ); Query OK, 0 rows affected (0.69 sec)
使用 insert 命令在表中插入一些记录 −
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

