how update value between 2 strings, please ?

Discussion in 'MySQL' started by crazy.works, May 5, 2011.

  1. #1
    hello, i want to update data between two strings.

    phrase = "string1 data here string2"
    phrase = "string1 different data string2"
    phrase = "string1 whole different data string2"

    so i have multi rows, i wanna use one query to update all the data between "string1" and "string2"..
    and after the update, the phrases will be like this:-

    phrase = "string1 my data string2"
    phrase = "string1 my data string2"
    phrase = "string1 my data string2"

    so any idea about the query please ?
     
    Last edited: May 5, 2011
    crazy.works, May 5, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You would need to use a regex however, mysql doesn't support using a regex in an update situation unless you recompile it with an additional extension. http://www.mysqludf.org/lib_mysqludf_preg/

    I would write a php or other script to loop through and update the rows.
     
    jestep, May 5, 2011 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi,

    There is a solution, because this is a particular case (as described) :
    
    DROP TABLE IF EXISTS TESTING;
    
    CREATE TABLE TESTING(phrase VARCHAR(255));
    
    INSERT INTO TESTING (phrase) VALUES('string1 data here string2'),
    ('string1 different data string2'),
    ('string1 whole different data string2'),
    ('string3 data here string4');
    
    SELECT * FROM TESTING;
    
    Code (markup):
    
    UPDATE TESTING SET phrase='string1 my data string2' WHERE 
    TRIM(phrase) REGEXP '^string1[[:>:]].*[[:<:]]string2$';
    
    SELECT * FROM TESTING;
    
    Code (markup):
    Regards :)
     
    koko5, May 5, 2011 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    UPDATE tablename SET phrase=CONCAT('string1', ' mydata ', 'string2') WHERE phrase LIKE 'string1%string2';
     
    plog, May 5, 2011 IP
  5. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this only works for phrase = "string1 data here string2" ...
    but it doesn't work for phrase = "something_here string1 data here string2 another_thing" !!!
     
    crazy.works, May 5, 2011 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    It doesn't work for a lot of things (it won't increase your gas mileage, it won't clear up your acne, it will have little to no affect on the size and girth of your member), but for the initial question and examples you posted it does.
     
    plog, May 6, 2011 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    LIKE 'string1%string2';

    is not going to match

    "something_here string1 data here string2 another_thing" !!!

    You could use LIKE '%string1%string2%';

    But then you replace something_here string1 and string2 another_thing with string1 and string2 and don't retain the something_here and another_thing values.

    You need to loop through using a regex. The mysql extension or using a script to do it is the only way.
     
    jestep, May 6, 2011 IP