我正在尝试将新行插入到我的MySQL表中,除非for
列中具有相同值的行存在,在这种情况下我想更新该行。
我的疑问是:
var sum = 0;
var newArray = [];
for (var i=0; i<a.length;i++){
for (var j=0;j<a.length;j++){
if(a[i]['taxid']==a[j]['taxid']){
sum = a[i]['tax_value'] + a[j]['tax_value'];
}
}
newArray.push({taxid : a[i]['taxid'], tax_value : sum});
}
和表结构如下:
campign_id
现在我明显错误地认为,因为INSERT INTO prv_campaigns SET
website_id = %s,account_id = %s,campaign_id = %s,campaign_name = %s,start_date = %s,
end_date = %s,targeting_type = %s,enabled = %s,status = %s,bid_type = %s,
default_bid = %s,min_bid = %s,max_bid = %s,budget = %s,cost = %s,ctr = %s,
utm_codes = %s,country_codes = %s,country_targeting = %s,opt = %s,
device_targeting = %s,language_targeting = %s,exclude_low_volume = %s
ON DUPLICATE KEY UPDATE
campaign_name = %s,
start_date = %s,
end_date = %s,
targeting_type = %s,
enabled = %s,
status = %s,
bid_type = %s,
default_bid = %s,
min_bid = %s,
max_bid = %s,
budget = %s,
cost = %s,
ctr = %s,
utm_codes = %s,
country_codes = %s,
country_targeting = %s,
opt = %s,
device_targeting = %s,
language_targeting = %s,
exclude_low_volume
是一个独特的键,然后查询的更新部分将发生,而是在我的数据库中创建一个新行...任何想法如何解决这个问题? THX
答案 0 :(得分:1)
campaign_id
不是唯一键。它是四部分唯一键的一个元素。四个组合是唯一的。不是每个人都单独。
如果您希望它是唯一的,请将其声明为:unique (campaign_id)
。您可能还希望其他字段“单独”唯一。
如果您愿意,可以将此约束添加到表中:
alter table prv_campaigns add constraint unq_campaigns_campaign_id
unique (campaign_id);