如何用MySQL修剪逗号?
语法如下以修剪逗号-
SELECT TRIM(BOTH ',' FROM yourColumnName) from yourTableName;
让我们看一个例子-
mysql> create table TrimCommasDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> AllTechnicalSkills text -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into TrimCommasDemo(AllTechnicalSkills) values(',C,C++,Java,'); mysql> insert into TrimCommasDemo(AllTechnicalSkills) values(',MySQL,SQL Server,MongoDB,'); mysql> insert into TrimCommasDemo(AllTechnicalSkills) values(',Spring Framework,Hibernate Framework,');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from TrimCommasDemo;
这是输出-
+----+----------------------------------------+ | Id | AllTechnicalSkills | +----+----------------------------------------+ | 1 | ,C,C++,Java, | | 2 | ,MySQL,SQL Server,MongoDB, | | 3 | ,Spring Framework,Hibernate Framework, | +----+----------------------------------------+ 3 rows in set (0.00 sec)
以下是从MySQL的开头和结尾修剪逗号的查询-
mysql> select TRIM(BOTH ',' FROM AllTechnicalSkills) from TrimCommasDemo;
这是输出-
+----------------------------------------+ | TRIM(BOTH ',' FROM AllTechnicalSkills) | +----------------------------------------+ | C,C++,Java | | MySQL,SQL Server,MongoDB | | Spring Framework,Hibernate Framework | +----------------------------------------+ 3 rows in set (0.00 sec)