SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF NOT EXISTS INSERT, ELSE UPDATE -- Is it possible?

    Hey, Im wondering if you can do this in SQL. I have searched google and can't find what I'm looking for to explain it to me.

    Is this possible? What am I doing wrong?
    Or is this something I should do through PHP with multiple queries?

    Basically it's a conditional statement.
    These are fake values, Im just trying to learn if it can be done
    Code MySQL:
        IF 
        NOT EXISTS 
            (
            SELECT user_id, bot_id 
            FROM tmp_battles 
     
            WHERE user_id = 1 
            AND bot_id = 5
            ) 
     
        THEN 
     
            INSERT INTO tmp_battles 
            SET  
            user_id = 1,
            bot_id = 2
     
     
     
        ELSE
     
            UPDATE tmp_battles
            SET
            user_id = 1,
            bot_id = 55,
     
            WHERE user_id = 10
            AND bot_id = 55
     
        END IF

    Or what about something like this:

    Code MySQL:
     IF 
       (SELECT COUNT(*) FROM items WHERE id = 1) > 10
    THEN
        DO SOMETHING
    ELSE
       DO SOMETHING ELSE

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    yes, it can be done

    check in the mysql manual for the INSERT statement with ON DUPLICATE KEY UPDATE clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for reply Rudy. I am looking at the ON DUPLICATE KEY right now.
    My question I suppose is,

    With an AUTO_INCREMENT column named id as my PRIMARY KEY
    This probably will never have a duplicate.

    Is there a way to check if there is a duplicated of assign_bot and assign_user?

    Code MySQL:
                INSERT INTO tmp_battles
                SET 
                `assign_bot` = '$bot[id]',
                `assign_user` = '{$sess->get('userID')}',
                `battle_hp` = '$bot[hp]'
     
                ON DUPLICATE KEY 
                    UPDATE `battle_hp` = '$bot[hp]'

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Add another unique key (assing_bot + assign_user) to the table.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •