只返回 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)
广告
数据结构
网络技术
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP