MySQL update number to make partially random

Discussion in 'MySQL' started by Astroman, Dec 31, 2008.

  1. #1
    Does anyone know how I can replace a number, in this case a year, to make it partially random? For instance I want a whole range of numbers within the 1980s and currently have every row showing 1980, so need somthing like

    UPDATE tablename SET tablefield = replace(tablefield,"1980","198*rnd");
    Code (markup):
    Only I don't know how to work the *rnd bit to make the figure after 198?
     
    Astroman, Dec 31, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    UPDATE tablename SET 
    tablefield = replace(tablefield,'1980',CONCAT('198', FLOOR(RAND()*10)));
    Code (markup):
     
    mwasif, Jan 1, 2009 IP
  3. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Thanks, that's brilliant! I wasn't even sure it was possible, database stuff blows me away sometimes, which makes me feel such a geek.

    I would give you a +rep but it wont let me because I just gave you a +rep recently.

    I started a little PHP help site of my own recently that's more for my own reference than anything else, and decided to put a MySQL snippet I learned last night on there because I keep learning stuff then forgetting where I saw it. Anyway I just put the code with your correction on just now: http://www.phpecho.com/mysql-tutorials/updatemysqlfieldrandomnumber.html - so I have two snippets so far. :)
     
    Astroman, Jan 1, 2009 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Welcome.

    Try to give the +rep now :)
     
    mwasif, Jan 2, 2009 IP
  5. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #5
    I tried, it says I have to spread it around before I can rate you again. :)
     
    Astroman, Jan 2, 2009 IP