简体   繁体   English

替换多个mysql数据库列中的字符

[英]Replace a character in multiple mysql database column

Hello I am new to mySQL and i am having trouble trying extracting data from my db by using keywords. 您好,我是mySQL的新手,尝试使用关键字从数据库中提取数据时遇到麻烦。 I have 2 columns (tags and categories). 我有2列(标签和类别)。 I have done this before using the LIKE '%keyword%' in my query. 在查询中使用LIKE'%keyword%'之前,我已经完成了此操作。 The problem is in the db i am using now each keyword is separated with a ';' 问题出在我正在使用的数据库中,每个关键字都用“;”分隔 i would like to know if it is possible to somehow separate each keyword by replacing the semi-colon with a space? 我想知道是否可以通过用空格替换分号来分隔每个关键字? Or if it's easier use the same query but instead of using '%' to separate i use semi-colons? 或者,如果更容易使用相同的查询,而不是使用'%'进行分隔,我使用分号? sorry if its a newbie question but i am struggling hard with this and really need some help. 抱歉,如果这是一个新手问题,但我为此付出了很多努力,确实需要一些帮助。

Thanks 谢谢

您可以使用REPLACE函数将分号替换为空格(或其他任何空格):

UPDATE MyTable SET tags = REPLACE(tags,';',' '), categories = REPLACE(categories,';',' ')

The problem is in the db i am using now each keyword is separated with a ';' 问题出在我正在使用的数据库中,每个关键字都用“;”分隔 i would like to know if it is possible to somehow separate each keyword by replacing the semi-colon with a space? 我想知道是否可以通过用空格替换分号来分隔每个关键字? Or if it's easier use the same query but instead of using '%' to separate i use semi-colons? 或者,如果更容易使用相同的查询,而不是使用'%'进行分隔,我使用分号?

Example table: 表格示例:

CREATE TABLE `test` (  
  `tags` varchar(255) NOT NULL,
  `categories`  varchar(255) NOT NULL  
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Sample data: 样本数据:

insert into `test` values ('tag', 'cat' );
insert into `test` values ('tag', 'cat1;cat2;cat3;' );

Example select: 示例选择:

select * from `test` where `categories` LIKE '%cat1;%';
+------+-----------------+
| tags | categories      |
+------+-----------------+
| tag  | cat1;cat2;cat3; |
+------+-----------------+
1 row in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM