要获取子字符串,请substr()
按照以下语法使用MySQL中的方法-
select substr(yourColumnName,startIndex,endIndex) from yourTableName limit anyValue; select substr(yourColumnName,startIndex+endIndex) from yourTableName limit anyValue;
让我们创建一个表-
mysql> create table demo11 −> ( −> id int NOT NULL AUTO_INCREMENT PRIMARY KEY, −> fileLocation text −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo11(fileLocation) values('E:/users/program/sample.sql'); mysql> insert into demo11(fileLocation) values('E:/users/data/db.sql'); mysql> insert into demo11(fileLocation) values('C:/users/data/sample2.sql');
使用select语句显示表中的记录-
mysql> select *from demo11;
这将产生以下输出-
+----+-----------------------------+ | id | fileLocation | +----+-----------------------------+ | 1 | E:/users/program/sample.sql | | 2 | E:/users/data/db.sql | | 3 | C:/users/data/sample2.sql | +----+-----------------------------+ 3 rows in set (0.00 sec)s
这是获取多行子字符串结果的查询。
第一部分查询如下-
mysql> select substr(fileLocation,1,15) from demo11 limit 1,2;
这将产生以下输出-
+---------------------------+ | substr(fileLocation,1,15) | +---------------------------+ | E:/users/data/d | | C:/users/data/s | +---------------------------+ 2 rows in set (0.00 sec)
第二部分如下-
mysql> select substr(fileLocation,16) from demo11 limit 1,2;
这将产生以下输出-
+-------------------------+ | substr(fileLocation,16) | +-------------------------+ | b.sql | | ample2.sql | +-------------------------+ 2 rows in set (0.00 sec)