MySQL删除包含特定单词的行

时间:2018-03-21 11:33:48

标签: mysql sql

我试图从包含特定字词的所有行中删除特定列中的行。

例如:

删除包含单词apple的行,它始终位于该行的开头。

+--+------------------+
|ID|data              |
+--+------------------+
|1 |sometext1         |
|  |sometext2         |
|  |apple sometext3   |
|  |sometext4         |
+--+------------------+
|2 |apple sometext5   |
|  |sometext6         |
+--+------------------+

所以结果将是:

+--+------------------+
|ID|data              |
+--+------------------+
|1 |sometext1         |
|  |sometext2         |
|  |sometext4         |
+--+------------------+
|2 |sometext6         |
+--+------------------+

' SometextX'每行都不同,每行的行数不同,每行的字符数不同。 我真的需要在MySQL中提供任何帮助。

4 个答案:

答案 0 :(得分:1)

最好在这里使用DELETE FROM yourTable WHERE text REGEXP '^apple'; 来匹配每行中的模式:

REGEXP

apple允许相当复杂的正则表达式匹配,如果您的需求发生变化或以后变得更复杂,它将非常有用。

编辑:MySQL没有内置的正则表达式替换支持,因此没有简单的方法可以实现您的目标。

删除单词\bapple\b的常规正则表达式模式为ILinkManager fLinkManager = (ILinkManager) repo.getClientLibrary(ILinkManager.class); IReferenceFactory fReferenceFactory = fLinkManager.referenceFactory(); IReference reference = fReferenceFactory.createReferenceToItem(workItem.getItemHandle()); ILinkQueryPage page = fLinkManager.findLinksByTarget("com.ibm.team.filesystem.workitems.change_set", reference, monitor); ILinkCollection linkCollection = page.getAllLinksFromHereOn(); Collection<ILink> links = linkCollection.getLinksById("com.ibm.team.filesystem.workitems.change_set"); 。您可以搜索此模式并替换为空字符串。

答案 1 :(得分:0)

您可以使用<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <exportAntProperties>true</exportAntProperties> <target> <exec executable="svn" outputProperty="svn.info"> <arg value="info"/> <arg value="--xml"/> </exec> </target> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>regex-svn-last-rev</id> <goals> <goal>regex-property</goal> </goals> <configuration> <!-- Output property --> <name>svn.last.rev</name> <value>${svn.info}</value> <regex>^[\s\S]*?commit[\s\S]*?revision=\"(\d+?)\"[\s\S]*?$</regex> <replacement>$1</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <SCM-Revision>${svn.last.rev}</SCM-Revision> </manifestEntries> </archive> </configuration> </plugin>

where

这可以是where textcol not like 'apple%' or textcol is null select的一部分(问题提及&#34;结果&#34;这表明前者和&#34;删除&#34;这表明后者)。目前尚不清楚您是否真的想要更改数据,或者您是否只想要没有这些词的结果集。

注意:您可以在没有delete且仍处理or值的情况下执行此操作,因为MySQL具有NULL - 安全等式运算符:

NULL

答案 2 :(得分:0)

您可以使用MySQL functions选择正确的行,并使用以下新数据进行更新:

UPDATE `yourTable` SET `yourField` = REPLACE(yourField, 'apple', '') WHERE yourField LIKE '%apple%'

答案 3 :(得分:0)

如果您不想删除整行,可以按此顺序运行这3个查询

update your_table set text=replace(text,substring(text,@start:=locate('\napple',text),locate('\n',text,@start+1)-@start+1),'');
update your_table set text=if((@start:=locate('apple',text))=1,replace(text,substring(text,@start,locate('\n',text,@start+1)-@start+1),''),text);
update your_table set text=if((@start:=locate('apple',text))=1,replace(text,substring(text,locate('apple',text)),''),text);

更新#1将删除文本中间的 (以\n为前缀) 更新#2将在其行的开头中删除apple (之前没有任何内容)并且具有以下行 更新#3将删除剩余的案例