MySQL Syntax Error ON DUPLICATE KEY UPDATE

Hey guys, I was wondering if anyone could spot errors in my sql statement. I would like for him to insert a new record into my table if it is not already there. If it exists, just update it. My primary key is in the date field.

here is my expression, how it appears in php, as well as the error i get:

INSERT INTO ExtraStats (date, supportStaff, startEmails, endEmails, emailsAnswered) VALUES ('$startDate', '$supportStaff', '$startEmail', '$endEmail', '$emailAnswered') ON DUPLICATE KEY UPDATE (supportStaff, startEmails, endEmails, emailsAnswered) VALUES ('$supportStaff', '$startEmail', '$endEmail', '$emailAnswered')

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(supportStaff, startEmails, endEmails, emailsAnswered) VALUES ('2', '3', '1', '3' at line 1 
+3
source share
2 answers
INSERT INTO ExtraStats (
    DATE
    , supportStaff
    , startEmails
    , endEmails
    , emailsAnswered
    )
VALUES (
    '$startDate'
    , '$supportStaff'
    , '$startEmail'
    , '$endEmail'
    , '$emailAnswered'
    )
    ON DUPLICATE KEY

UPDATE 
        supportStaff = '$supportStaff'
        , startEmails = '$startEmail'
        , endEmails = '$endEmail'
        , emailsAnswered =  '$emailAnswered'

You can also use the function VALUES()to not pass values ​​twice:

    ...
    ON DUPLICATE KEY    
UPDATE 
          supportStaff = VALUES(supportStaff)
        , startEmails = VALUES(startEmails)
        , endEmails = VALUES(endEmails)
        , emailsAnswered = VALUES(emailsAnswered)
+5
source

date is a reserved word, encloses it with reverse windows:

INSERT INTO ExtraStats (`date`, supportStaff, startEmails, etc...

It ON DUPLICATE KEY UPDATEshould also look something like this:

ON DUPLICATE KEY UPDATE supportStaff = '$supportStaff', startEmails = '$startEmail', etc..

. , , SQL.

0

Source: https://habr.com/ru/post/1606678/


All Articles