繁体   English   中英

SQL查询-替换/移动内容的某些部分

[英]SQL query - Replace/Move some parts of content

我需要在MySQL中更新约2000条记录

我在表“ my_table”中有一个列“ my_content”,具有以下值

Title: some title here<br />Author: John Smith<br />Size: 2MB<br />

我创建了3个新列(my_title,my_author和my_size),现在我需要像这样分隔“ my_content”的内容

“ my_title”

some title here

'my_author'

John Smith

'my_size'

2MB

可以想象,每一行的标题,作者和大小总是不同的。

我想查询的是以下内容,但我对SQL查询并不满意,并且不确定实际查询的外观。

这就是我想要做的:

Within 'my_content' find everything that starts with "title:..." and ends with "...<br />au" and move it to 'my_title'
Within 'my_content' find everything that starts with "thor:..." and ends with "...<br />s" and move it to 'my_author'
Within 'my_content' find everything that starts with "ize:..." and ends with "...<br />" and move it to 'my_size'

我只是不知道如何编写查询来执行此操作。

一旦所有内容都在新列中,我就可以找到并删除不再需要的内容,例如'thor:'等。

您可以使用INSTR查找定界符的索引,并使用SUBSTRING选择所需的零件。 因此,例如,作者将是

SUBSTR(my_content,
       INSTR(my_content, "Author: ") + 8,
       INSTR(my_content, "Size: ") - INSTR(my_content, "Author: ") - 8)

您需要做更多的工作来修剪<br/>和周围的空白。

请尝试以下方法:

SELECT SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',1),LOCATE('Title: ',mycontent)+7) as mytitle, 
SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',2),LOCATE('Author: ',mycontent)+8) as myauthor, 
SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',3),LOCATE('Size: ',mycontent)+6) as mysize 
FROM mytable;

暂无
暂无

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

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