【问题标题】:Syntax error on MERGE statementMERGE 语句的语法错误
【发布时间】:2012-11-29 11:27:40
【问题描述】:

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

怎么了?

【问题讨论】:

  • MySQL 没有 MERGE 语句。

标签: mysql sql


【解决方案1】:

MySQL 不支持MERGE,等效于

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

【讨论】:

  • 这是一个通用的应用程序,可以在两个引擎中使用
  • 我能做的最好的就是检查电子邮件是否存在,然后使用 INSERT 或 UPDATE。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多