On Duplicate Key Update - Multiple Columns

后端 未结 4 1900
谎友^
谎友^ 2020-12-08 14:34

When using insert... on duplicate key update, what is the syntax to update multiple columns?

INSERT INTO table1 (col1, col2, col3, col4) VALUES (’$val1’, ‘$v         


        
相关标签:
4条回答
  • 2020-12-08 14:44
    INSERT INTO table1
      (`col1`, `col2`, `col3`, `col4`)
    VALUES
      ('val1', 'val2', 'val3', 'val4')
    ON DUPLICATE KEY UPDATE
      `col2`='val2',
      `col3`='val3', [...]
    

    I fixed your quotes and tickmarks.

    Edit:

    In PHP:

    $result = mysql_query("
         INSERT INTO table1
             (col1, col2, col3, col4)
         VALUES
             ('" . $val1 . "',  '" . $val2 . "', '" . $val3 . "', '" . $val4 . "')
         ON DUPLICATE KEY UPDATE
             col2='" . $val2 . "',
             col3='" . $val3 . "',
             col4='" . $val4 . "'"
     );
    

    Note that the values are surrounded by single quotation marks '. If the values are a number type (INT, FLOAT, etc) you can drop those quotation marks. Backticks are optional around the column names as long as you are not using column names like count, type, or table.

    In the PHP example, string concatenation is used to clearly separate out the variables.

    0 讨论(0)
  • 2020-12-08 14:58

    Your query seems to be correct. Here is my example of this type of query:

    INSERT INTO Stat (id, month, year, views, redirects, onList, onMap, emails) VALUE ("' . $Id . '","' . $month . '","' . $year . '",0,0,"' . $data['onList'] . '","' . $data['onMap'] . '",0) ON DUPLICATE KEY UPDATE onList=onList+' . $data['onList'] . ', onMap=onMap+' . $data['onMap']
    
    0 讨论(0)
  • 2020-12-08 15:03

    Well, this is old. But of course you only need to provide a value once, there's no reason to add it a second time in the query (which comes in handy for multiple inserts, or prepared statements):

    INSERT INTO table1
      (col1, col2, col3, col4)
    VALUES
      ('val1', 'val2', 'val3', 'val4')
    ON DUPLICATE KEY UPDATE
      col2=VALUES(col2),
      col3=VALUES(col3) [,...]
    

    Which has as advantage it will still work for a multiple insert statement:

    INSERT INTO table1
      (col1, col2, col3, col4)
    VALUES
      ('val1', 'val2', 'val3', 'val4'),
      ('val5', 'val6', 'val7', 'val8'),
      ('val9', 'val10', 'val11', 'val12')
    ON DUPLICATE KEY UPDATE
      col2=VALUES(col2),
      col3=VALUES(col3) [,...]
    
    0 讨论(0)
  • 2020-12-08 15:11

    For the sake of clear syntax there's another syntax form;

    INSERT INTO `table1` SET `id`=$id,
                             `col2`='$col2',
                             `col3`='$col3'[, ...]
     ON DUPLICATE KEY UPDATE `col2`='$col2',
                             `col4`='$col4'[, ...]
    

    Example;

    INSERT INTO customers SET cid=10,
                              createdon=NOW(),
                              createdby='user',
                              cname='Steve'
      ON DUPLICATE KEY UPDATE modifiedon=NOW(),
                              modifiedby='user',
                              cname='Steve';
    

    If there does not exist a customer with ID=10 in database it will be created and columns cid, createdon, createdby, cname will be set. If it does exist, then it will be updated and columns modifiedon, modifiedbym, cname will be updated.

    NOTE#1: IF you put for primary key cid=0 here, it will fire AUTO_INCREMENT (of course, if pk column is defined as AUTO_INCREMENT) and a record will be inserted!

    NOTE#2: ON DUPLICATE KEY UPDATE makes update for existing PK ID record. But also it makes update if DUPLICATE is made on any UNIQUE KEY column. For example, if you defined that cname column is UNIQUE, then saving record with cname='Steve' that already exist will result in UPDATE of that record (not new INSERT). Take care about this because you may expect that DB returns error for UNIQUE KEY constraint violation which will not happened here.

    0 讨论(0)
提交回复
热议问题