Delete records in MySQL that contains certain words

Discussion in 'MySQL' started by jehzlau, Jun 9, 2010.

  1. #1
    Hi,

    I'm a MySQL noob and I want to delete some records in a specific table that contains specific words.

    For example, if this record has the words strawberry, apple and banana, I want to select it all and delete it all.

    How can I do it? :(

    I tried this:
    DELETE * FROM [table name] WHERE [field name] = "strawberry, apple, banana";
    Code (markup):
    But it was wrong... :(
     
    Last edited: Jun 9, 2010
    jehzlau, Jun 9, 2010 IP
  2. listener86

    listener86 Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    DELETE * FROM [table name] WHERE [field name] like "%strawberry%" or [field name] like "%apple%", or [field name] like "%banana%";

    It may be very slow, but will work. You can try to use regexp also.
     
    listener86, Jun 9, 2010 IP
  3. cDc

    cDc Peon

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Take care here! :) this query would also delete rows containing pineapples! You might not want this. Add a space to cover this. So the syntax should be like this...

    DELETE FROM [table name] WHERE ( [field name] like '% strawberry%' or [field name] like '% apple%' or [field name] like '% banana%')

    Backup first before running "like" deletes :)
     
    cDc, Jun 10, 2010 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Does the field name contain other text as well, or just 1 word? If it is just 1 word, you would want to do it a little different. LIKE queries take a lot more resources than a standard column = value. If you have multiple values, you can use:

    DELETE FROM [table name] WHERE [field name] IN ('strawberry', 'apple', 'banana');
     
    jestep, Jun 10, 2010 IP