MySQL Command Help

Discussion in 'MySQL' started by NaSh123, Jan 18, 2008.

  1. #1
    Basic situation is I have a database setup with a table called submitted. Each row has about 7 different variables. One of the variables is called image. There are over 700 rows and what I want to do is....

    Each image variable has the following setup...

    ../images/thumbs/4.jpg

    What single command can I use to replace the "../images/thumbs/4.jpg" to just read "thumbs/4.jpg"
     
    NaSh123, Jan 18, 2008 IP
  2. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    UPDATE submitted SET image = 'thumbs/4.jpg' WHERE image = '../images/thumbs/4.jpg';
    Code (sql):
     
    Krnl, Jan 18, 2008 IP
  3. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know that would work for JUST the 4.jpg image but is there a way to have it scan through EVERY row and just change the preceding before the #.jpg?
     
    NaSh123, Jan 18, 2008 IP
  4. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #4
    sounds to me like you need to run a loop that will run a select query on each entry (is there an auto_increment value in the table that you can use to loop through the entries?), then run it through the php str_replace function - $new_value = str_replace("../images/", "", $result) - then update the table entry with the $new_value.
     
    Krnl, Jan 18, 2008 IP
  5. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes I do have an autoincrement.

    Could you possible give me the query that would be needed that would run the loop to then edit each of the values? I can't really have downtime so I'd rather have it done right the first time.
     
    NaSh123, Jan 18, 2008 IP
  6. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The following should work but I use MS SQL more than MySQL
    
    UPDATE submitted SET image = replace(image, '../images/', ''); 
    Code (SQL):
     
    AstarothSolutions, Jan 18, 2008 IP
  7. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What exactly would that replace? What would the before and after look in each row that currently has

    ../images/thumbs/#.jpg
     
    NaSh123, Jan 18, 2008 IP
  8. benajnim

    benajnim Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This would by my solution:

    update submitted set image = substr(image, 18);
    Code (sql):
    You can test to see what the field *would* look like before firing off the update:

    select substr(image, 18) from submitted limit 5;
    Code (sql):
     
    benajnim, Jan 18, 2008 IP
  9. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Which command would be executed first? What if the numbers go in double or tripple digits?
     
    NaSh123, Jan 18, 2008 IP
  10. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It would remove the ../image/ and replace it with nothing so you would end up with thumbs/#.jpg for each row irrespective how large the # is
     
    AstarothSolutions, Jan 18, 2008 IP
  11. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #11

    Worked like a charm. Thank you!
     
    NaSh123, Jan 18, 2008 IP
  12. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #12
    no problem, I keep telling our developers I know a little about what i am talking about but they wont believe me ;)
     
    AstarothSolutions, Jan 18, 2008 IP