gpt4 book ai didi

mysql - MERGE 语句的语法错误

转载 作者:行者123 更新时间:2023-11-29 01:06:56 25 4
gpt4 key购买 nike

我正在尝试在 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

怎么了?

最佳答案

MERGE 不被 MySQL 支持,对应的是

INSERT ... ON DUPLICATE KEY UPDATE

试试这个,

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

关于mysql - MERGE 语句的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13624752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com