#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Only insert if the field has no value already, otherwise skip it...


    As the title suggests, I'm trying to write mysql that will only insert a value if the field is empty otherwise it will ignore it and where the playername=something.

    So something like...

    Insert playeridvalue into tableA into columnplayerid where playername is brute only if the column playerid is empty though, otherwise skip.

    This has confused me A LOT! I'm not sure if to use if exists, insert...on duplicate key update, etc.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,927
    Rep Power
    378
    Code:
    UPDATE my_table SET my_column = 'something' WHERE playername IS 'another thing' AND my_column = '';
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    If there if something already there I don't want it to do anything to it, I don't want it to update/replace etc. That value should only be assigned once when the player first logs in and then read from then on only.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,927
    Rep Power
    378
    I don't really understand what you're after but perhaps "INSERT IGNORE" will suffice.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    As long as I leave the AND my_column = ''; blank it actually works quite nicely, thank you .

IMN logo majestic logo threadwatch logo seochat tools logo