multiple ON DUPLICATE KEY UPDATE

Here is an example of how to update multiple columns using values supplied in the INSERT statement. This assumes that column 'a' is the unique key.

INSERT INTO table (a,b,c,d,e) VALUES (1,2,3,4,5) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c), d=VALUES(d), e=VALUES(e);

This also works for multiple rows:

INSERT INTO table (a,b,c,d,e) VALUES (1,2,3,4,5), (6,7,8,9,10) ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c), d=VALUES(d), e=VALUES(e);

If you have a lot of columns it would be nice if you could use the following syntax:

INSERT INTO table (a,b,c,d,e) VALUES (1,2,3,4,5) ON DUPLICATE KEY UPDATE VALUES(b,c,d,e);

and it would match the columns you want to update with the values in the INSERT. Unfortunately, this does not work. You MUST explicitly provide each column assignment.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
 d8b      d8888              .d8888b.    .d8888b.    .d8888b.  
Y8P d8P888 d88P Y88b d88P Y88b d88P Y88b
d8P 888 .d88P .d88P .d88P
888 d8P 888 888 888 8888" 8888" 8888"
888 d88 888 888 888 "Y8b. "Y8b. "Y8b.
888 8888888888 Y88 88P 888 888 888 888 888 888
888 888 Y8bd8P Y88b d88P Y88b d88P Y88b d88P
888 888 Y88P "Y8888P" "Y8888P" "Y8888P"


Enter the code depicted in ASCII art style.

Similar

  • New technique to win olimpics -

    New technique to win olimpics

    At the 1968 Olympics, Dick Fosbury took the athletics world by surprise with an unusual high-jump technique.

  • Printing mysql_error -

    Printing mysql_error

    This is a code snipped to print mysql error.
    If there is no error mysql_error returns null, thus if evaluates false and there is no output.

  • multiple ON DUPLICATE KEY UPDATE -

    multiple ON DUPLICATE KEY UPDATE

    Here is an example of how to update multiple columns using values supplied in the INSERT statement. This assumes that column 'a' is the unique key.

  • multiple ON DUPLICATE KEY UPDATE -

    multiple ON DUPLICATE KEY UPDATE

    Here is an example of how to update multiple columns using values supplied in the INSERT statement. This assumes that column 'a' is the unique key.

  • php unlink recursive -

    php unlink recursive

    The shortest recursive delete possible.

    <?php
    /**
    * Delete a file or recursively delete a directory
    *
    * @param string $str Path to file or directory
    */