40

What's the command to achieve this:
MYSQL: delete all rows containing string "foo" in table "bar"

8 Answers 8

71
DELETE FROM bar where

field1 like '%foo%' 
OR
field2 like '%foo%'
OR
...
fieldLast like '%foo%'
Sign up to request clarification or add additional context in comments.

4 Comments

wouldn't this remove rows with fooo or fooh value?
@ToniMichelCaubet yes, fooo or foooh are string that contain foo.
True! It's just that I ended up here looking for a different need ;) Sorry about that
you could try like '% foo' or like '% foo %' or like 'foo %' that would match foo surrounded by spaces...
8

You'll need to explicitly list the columns I think, so something along the lines of...

 DELETE FROM bar WHERE col1 LIKE '%foo%' OR col2 LIKE '%foo%'....etc

Comments

8

Try this query

 delete from [table name] where [column name] like '%[text to find]%'

This will match if the text appears, regardless of position. i.e.

if you were looking for foo, it would match "xfoo" and "foox"

Comments

2

Normally, you can use the 'LIKE' keyword to perform simple pattern matching: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

Comments

1

The query and explaination can be found here (see question 2)

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/the-ten-most-asked-sql-server-questions--1#2

You may also need to change the comparative condition to "like" condition.

Comments

0
delete from bar where field1 like '%foo%' OR field2 like '%foo%' OR ... fieldLast like '%foo%'

1 Comment

While this post may answer the question, it is still a good idea to add some explanation and, possibly, some links to the relevant documentation. Answers with good explanations and references are usually more useful both to the current OP and to the future visitors. Full detailed answers are also more likely to attract positive votes.
0

you can use "IN" operator if you want to delete all row contain a specific value in any column:

DELETE FROM bar where field1 in ( '%foo%','%anothertext%','%anothertext%')

Comments

0

You can also use LOCATE().

DELETE FROM bar WHERE LOCATE('foo', col1)<>0 OR LOCATE('foo', col2)<>0 ... etc

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.