MySQL Forums
Forum List  »  Newbie

insert on duplicate with a where clause on a field not part of the unique key
Posted by: Ravi Malghan
Date: June 11, 2017 10:43AM

Looking for some assistance with a insert statement on duplicate key. I have this table

CREATE TABLE IF NOT EXISTS `alarm` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_id` varchar(30) NOT NULL,
  `hostname` varchar(32) NOT NULL,
  `status` tinyint(1) DEFAULT '0',
  `clearedtime` datetime default NULL,
  `count` tinyint(1) DEFAULT '0', 
  PRIMARY KEY (`id`),
  UNIQUE KEY `no_duplicate` (`event_id`)
)

I want to insert a new row if a row with same event_id exists and clearedtime is not null. clearedtime cannot be part of the unique key. If the row exists with a clearedtime null, I want to increment the count by 1. Can't seem to figure out how to include claredtime != null

insert into alarm (event_id, hostname, status,clearedtime, count) values ('event1', 'hosta', 5, null, 1) on duplicate key update count=count+1

Options: ReplyQuote


Subject
Written By
Posted
insert on duplicate with a where clause on a field not part of the unique key
June 11, 2017 10:43AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.