i have a table with 3 generic keys which are also foreign keys. This is my query --
IF (EXISTS(SELECT * FROM table1 WHERE col_1 =4))
BEGIN
UPDATE table1
SET col_2 = 3,
col_3 = 100
WHERE col_1 = 4
END
ELSE
BEGIN
INSERT INTO table1
(col_1, col_2, col_3)
VALUES(4, 2, 27)
END
This gives me a syntax error. Engine used InnoDB. Collation: utf8_swedish_ci
I tried this too --
INSERT INTO table1
(col1, col2, col3)
VALUES
(:val1, :val2, :val3)
ON DUPLICATE KEY UPDATE
col2=:val2,
col3=:val3
This doesn't work properly and only insert the rows inspite of having duplicate keys.
INSERT
withON DUPLICATE KEY UPDATE
? – Virginavirginalreplace
. you can may use this – EarleanearleenON DUPLICATE
isn't working because the "key" is all three columns. Probably how I'd deal with this is - attempt the update, and if nothing was affected, insert a new row (which still requires the table be locked, unfortunately). – Barksdale