MYSQL UPDATE command

Discussion in 'PHP' started by jacka, Feb 6, 2008.

  1. #1
    Hi

    I have a small problem in finding and replacing text in a field.

    I know how to use UPADTE command and have lookup up regexp, but I can not combine them together.
    I wish to change every occurrence of a myfield in mytable that has got QF in it to change it to GFI.
    a typical data is : abcQFxyz to chnage to abcQFIxyz.

    I got as far as:
    UPDATE mytable SET myfield = QFI WHERE part_number REGEXP 'QF';
    but I know that wrong.

    Thanks for your help.
     
    jacka, Feb 6, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    UPDATE `table_name` SET `field_name` = REPLACE(field_name, 'QF', 'GFI')
    
    Code (sql):
     
    nico_swd, Feb 6, 2008 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    BINGO.
    Thx:D
     
    jacka, Feb 6, 2008 IP
  4. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi
    I have another problem now.
    How can I only replace all the records that starts with QF0, QF1, QF2, (i.e. QF[0-9]) with QFI.
    I have tried this, but I know its wrong.
    
    update 'pn_pdf' SET 'part_number'= REPLACE (part_number,'regexp(^QF[0-9])','QFI')  ;
    
    Code (markup):
    many thanks
     
    jacka, Jun 12, 2008 IP
  5. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135