mysql - SQL : Insert Or Update command

标签 mysql sql

我正在使用“Voila Norbert”API,我想将结果存储在数据库表中,如下所示:

email_Norbert (
id    Integer(11) NOT NULL AUTO_INCREMENT,
idUser    Integer(11) NOT NULL,
email     VarChar(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci,
score     Integer(2),
existence TinyInt(1),
PRIMARY KEY (
        id
)

实际上我使用这个查询(作为示例,我设置了值,但它每次都会针对每个 idUser 进行更改):

INSERT INTO email_Norbert (idUser,email,score) 
VALUES (2 ,'MyEmailHere',100) 
ON DUPLICATE KEY UPDATE idUser = 2,email = 'MyEmailHere', score = 100

插入运行良好,但是当记录已经存在时,我确实添加了其他记录。

我应该做什么?

最佳答案

如果您希望同一用户的后续插入失败并进行更新,请在 iduser 列上添加唯一索引。

create unique index email_Norbert_u1 on email_Norbert(iduser)

关于mysql - SQL : Insert Or Update command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47199213/

相关文章:

php - 将 mysql_num_rows 分成 6x4

mysql - 在字符串表mysql语句中查找数字

mysql - MySQL 中的排名函数

php - mysql中的有限总和

MySQL - 仅选择最后修改的(或 NULL 值)

mysql - 如何在sql中将字符串附加到表中的值

php - 删除级联中的实体在多对多关系中不起作用

python - 使用 f-string 的查询格式

mysql - 基于三个关系表连接一个报表

c# - 将绑定(bind)的组合框添加到数据 GridView