Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error on MERGE statement

Tags:

sql

mysql

I'm trying to do a MERGE statement in Go:

query := "MERGE staged ON (email=$1)" +
" WHEN NOT MATCHED THEN INSERT (email, secret, passwd, ts, newAcct)" +
" VALUES($1,$2,$3,$4,TRUE)" +
" WHEN MATCHED THEN UPDATE staged SET" +
" newAcct=TRUE, existingUser=NULL, secret=$2, ts=$4"

_, err := db.Exec(query, email, secret, passwd, time.Now())

but I got this error:

pq: S:"ERROR" F:"scan.l" R:"scanner_yyerror" L:"1001" M:"syntax error at or near \"MERGE\"" P:"1" C:"42601"

The same happens in MySQL:

Error 1064: 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 'MERGE staged ON (email=?) WHEN NOT MATCHED THEN INSERT (email, secret, passwd, t' at line 1

What's wrong?


1 Answers

MERGE is not supported by MySQL, The equivalent for that is

INSERT ... ON DUPLICATE KEY UPDATE

Try this,

INSERT INTO tableName (email, secret, passwd, ts, newAcct) 
VALUES ($1,$2,$3,$4,TRUE)
ON DUPLICATE KEY UPDATE newAcct=TRUE, existingUser=NULL, secret=$2, ts=$4

but be sure email is set as Primary Key or Unique.

like image 74
John Woo Avatar answered May 30 '26 20:05

John Woo