Mysql 仅当第二个条件为真时才更新重复密钥
如果第二个条件为真,我该如何更新?我的mysql版本似乎不允许在重复语法的末尾使用WHEREMysql 仅当第二个条件为真时才更新重复密钥,mysql,Mysql,如果第二个条件为真,我该如何更新?我的mysql版本似乎不允许在重复语法的末尾使用WHERE INSERT INTO `proxies` (`proxy`,`response`,`PAYMENT`,`type`,`country`,`status`,`tier`,`last_checked`,`last_active`,`response_time`) VALUES ('111.9.204.96:8123','200','coolproxies','anon','China','active'
INSERT INTO `proxies` (`proxy`,`response`,`PAYMENT`,`type`,`country`,`status`,`tier`,`last_checked`,`last_active`,`response_time`)
VALUES ('111.9.204.96:8123','200','coolproxies','anon','China','active','3','1400624136','1400624136','1.577639')
ON DUPLICATE KEY UPDATE `response`='200',`response_time`='1.577639',`type`='anon',`country`='China',`status`='active',`tier`='2',`last_checked`='1400624137'
这工作正常,但我只需要在上次选中的位置<1400624137'
为真时更新
这就是执行此操作的查询的外观
INSERT INTO `proxies` (`proxy`,`response`,`PAYMENT`,`type`,`country`,`status`,`tier`,`last_checked`,`last_active`,`response_time`)
VALUES ('207.204.249.193:21320','200','scanner','anon','United States','active','1','1400633866','1400633866','1.59696')
ON DUPLICATE KEY UPDATE
`response_time` = IF(`last_checked` < '1400633866', '1.59696', `response_time`),
`status` = IF(`last_checked` < '1400633866', 'active', `status`),
`last_checked` = IF(`last_checked` < '1400633866', '1400633866', `last_checked`),
`last_active` = IF(`last_checked` < '1400633866', '1400633866', `last_active`);
插入'proxy'('proxy','response','PAYMENT','type','country','status','tier','last\u checked','last\u active','response\u time`)
值('207.204.249.193:21320','200','scanner','anon','United','active','1','1400633866','1400633866','1.59696')
关于重复密钥更新
`响应时间`=IF(`last\u checked`<'1400633866','1.59696','response\u time`),
`状态`=IF(`last_checked`<'1400633866','active','status`),
`last_checked`=如果('last_checked`<'1400633866','1400633866','last_checked`),
`last_active`=如果('last_checked`<'1400633866','1400633866','last_active`);
我认为您需要将此作为两条语句:
update . . .
where last_checked < '1400624137';
insert ignore into proxies(. . .);
更新。
上次检查的地方<'1400624137';
在代理中插入忽略(…);
(或者在重复密钥更新时使用
不执行任何操作)。在@GordonLinoff提到的两个查询中执行此操作的替代方法是使用中描述的方法,您可以使用IF
根据条件进行更新
INSERT INTO `proxies` (...)
VALUES (...)
ON DUPLICATE KEY UPDATE
my_column = IF(last_checked < '1400624137', VALUES(my_column), my_column)
插入“代理”(…)
值(…)
关于重复密钥更新
my_column=IF(上次选中的_<'1400624137',值(my_column),my_column)
基本上,如果
的作用是,如果为TRUE,则将字段更新为新值,否则如果为FALSE,则将其设置为当前值,这意味着不会更改现有数据