coderkid coderkid - 6 months ago 20
MySQL Question

Why can't this IF-STATEMENT work in MySQL?

INSERT INTO individual_key_log (stuff, stuff1, stuff2)

VALUES("YOUR_PK_NAME_HERE", 2, 3)

ON DUPLICATE KEY UPDATE stuff="YOUR_PK_NAME_HERE",

IF (1 > 0) THEN
SET stuff1 = 1;
ELSE
SET stuff1 = 2;
END IF;


How can I achieve the above logic? I'm using MariaDB.

Answer

Check IF syntax in manual. Your syntax is wrong. You can try this:

INSERT INTO individual_key_log (stuff, stuff1, stuff2) 
VALUES("YOUR_PK_NAME_HERE", 2, 3)
ON DUPLICATE KEY UPDATE stuff="YOUR_PK_NAME_HERE", stuff1 = IF(1 > 0, 1, 2)