MySQL INSERT IGNORE将1添加到非索引列

cof*_*tor 0 php mysql

我正在PHP循环中构建一个小报告.我在while()循环中运行的查询是这样的:

INSERT IGNORE INTO `tbl_reporting` SET datesubmitted = '2015-05-26', submissiontype = 'email', outcome = 0, totalcount = totalcount+1
Run Code Online (Sandbox Code Playgroud)

我希望totalcount每次运行查询时列都会递增.但是数字保持为1. UNIQUE索引组成前3列.

这是表格模式:

CREATE TABLE `tbl_reporting` (
 `datesubmitted` date NOT NULL,
 `submissiontype` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
 `outcome` tinyint(1) unsigned NOT NULL DEFAULT '0',
 `totalcount` mediumint(5) unsigned NOT NULL DEFAULT '0',
 UNIQUE KEY `datesubmitted` (`datesubmitted`,`submissiontype`,`outcome`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Run Code Online (Sandbox Code Playgroud)

当我将查询修改为常规UPDATE语句时:

UPDATE `tbl_reporting` SET totalcount = totalcount+1 WHERE datesubmitted = '2015-05-26' AND submissiontype = 'email' AND outcome = 1
Run Code Online (Sandbox Code Playgroud)

...有用.

难道INSERT IGNORE不允许添加数字?或者我的原始查询是否格式错误?

我想使用INSERT IGNORE,否则我将首先查询原始记录,然后插入,然后最终更新.

Mar*_*c B 5

想想你在做什么:

INSERT .... totalcount=totalcount+1
Run Code Online (Sandbox Code Playgroud)

要计算totalcount+1,数据库必须检索当前totalcount尚不存在的...的值,因为您正在创建一个新记录,并且没有现有数据可以从中检索"旧"值.

例如,你在去商店购买食材之前尝试吃蛋糕,更不用说混合/烘烤了.