一尘不染

MERGE语句上的语法错误

mysql

我正在尝试在Go中执行MERGE语句:

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())

但是我得到了这个错误:

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

在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

怎么了?


阅读 502

收藏
2020-05-17

共1个答案

一尘不染

MERGE不支持MySQL,等效的是

插入…在重复的密钥更新上

尝试这个,

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

但请确保email将其设置为Primary KeyUnique

2020-05-17