1
我試圖插入新行到我的MySQL表,除非存在campign_id
列中存在相同值的行,在這種情況下,我想更新行。 我的查詢是:MySQL的重複密鑰更新插入新行
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
和表結構如下:
CREATE TABLE `prv_campaigns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`creation_date` datetime DEFAULT CURRENT_TIMESTAMP,
`modification_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`website_id` int(11) DEFAULT '0',
`account_id` int(11) DEFAULT NULL,
`campaign_id` varchar(100) NOT NULL,
`campaign_name` varchar(256) NOT NULL,
`start_date` varchar(45) DEFAULT NULL,
`end_date` varchar(45) DEFAULT NULL,
`targeting_type` varchar(45) DEFAULT NULL,
`enabled` varchar(45) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`bid_type` varchar(45) DEFAULT NULL,
`default_bid` varchar(45) DEFAULT NULL,
`min_bid` varchar(45) DEFAULT NULL,
`max_bid` varchar(45) DEFAULT NULL,
`budget` varchar(45) DEFAULT NULL,
`cost` varchar(45) DEFAULT NULL,
`ctr` varchar(45) DEFAULT NULL,
`utm_codes` varchar(256) DEFAULT NULL,
`country_codes` json DEFAULT NULL,
`country_targeting` varchar(45) DEFAULT NULL,
`opt` varchar(45) DEFAULT NULL,
`device_targeting` json DEFAULT NULL,
`language_targeting` json DEFAULT NULL,
`exclude_low_volume` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ndx` (`website_id`,`account_id`,`campaign_id`,`campaign_name`)
) ENGINE=InnoDB AUTO_INCREMENT=14334 DEFAULT CHARSET=utf8$$
現在我顯然是錯誤的想法,既然campaign_id
是一個獨特的鍵,然後在查詢的更新部分會發生,而是在我的數據庫中創建一個新的行......任何想法如何解決這個問題? thx
假設我不能改變的表,有一個解決方案,我可以在提交查詢執行? –
@yarivbar。 。 。不是真的。如果'campaign_id'應該是唯一的,就應該這樣聲明。 –