MYSQL ON DUPLICATE KEY UPDATE没有更新 [英] MYSQL ON DUPLICATE KEY UPDATE didn't update

查看:386
本文介绍了MYSQL ON DUPLICATE KEY UPDATE没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于重复键声明的mysql.

I have a mysql on duplicate key statement.

mysql_query("INSERT INTO statistics (classify, apply) VALUES ('$classify', 1) 
ON DUPLICATE KEY UPDATE apply = apply + 1");

id  classify  apply

1   A         1

但是它没有更新现有行,而是继续添加另一行,这是哪里出了问题?

but it didn't update the existing row and it keep add another row, Where is the problem?

推荐答案

可能列classify不是unique.您需要在表中具有UNIQUE字段,以使ON DUPLICATE KEY UPDATE工作.如果尚未设置,则可以在下面执行此语句.

It's probably the column classify is not unique. You need to have a UNIQUE field in the table to make ON DUPLICATE KEY UPDATE work. If you have not set one, you can execute this statement below.

ALTER TABLE statistics ADD CONSTRAINT tb_uq UNIQUE (classify)

这篇关于MYSQL ON DUPLICATE KEY UPDATE没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆