在MySQL中用分隔符分隔字符串的左侧部分吗?
您可以使用MySQL的substring_index()函数分割字符串的左侧部分。语法如下-
SELECTyourColumnName1,.....N,SUBSTRING_INDEX(yourColumnName,’yourSeperatorSymbol’,1)
as anyVariableName from yourTableName;
值1表示您可以左移字符串的一部分。为了检查以上语法,让我们创建一个表。创建表的查询如下-
create table LeftStringDemo-> (
-> Id int,
-> Words varchar(100)
-> );
使用insert命令在表中插入一些记录。查询如下-
insert into LeftStringDemo values(1,'MySQL==6789');insert into LeftStringDemo values(2,'Java==Object Oriented');
insert into LeftStringDemo values(3,'C Language==Procedural Programming');
insert into LeftStringDemo values(4,'PL/SQL==Structured Programming');
使用select语句显示表中的所有记录。查询如下-
select *from LeftStringDemo;
以下是输出
+------+------------------------------------+| Id | Words |
+------+------------------------------------+
| 1 | MySQL==6789 |
| 2 | Java==Object Oriented |
| 3 | C Language==Procedural Programming |
| 4 | PL/SQL==Structured Programming |
+------+------------------------------------+
4 rows in set (0.00 sec)
这是在分隔符字符串'=='的基础上获取字符串列'Words'左侧部分的查询
select Id, substring_index(Words, '==', 1) as OnlyLefthandsideValue fromLeftStringDemo;
以下是输出
+------+-----------------------+| Id | OnlyLefthandsideValue |
+------+-----------------------+
| 1 | MySQL |
| 2 | Java |
| 3 | C Language |
| 4 | PL/SQL |
+------+-----------------------+
4 rows in set (0.00 sec)
以上是 在MySQL中用分隔符分隔字符串的左侧部分吗? 的全部内容, 来源链接: utcz.com/z/341218.html