ignore - Insert record if not exists in mysql -


may question asked many times, not working in case.

table schema

create table `tasks_taskstatus` (   `id` int(11) not null auto_increment,   `name` varchar(40) not null,   `video_status` tinyint(1) not null,   `creative_status` tinyint(1) not null default '0',   `missing_asset` tinyint(1) not null,   `is_finished` tinyint(1) not null,   `share_with_am` tinyint(1) not null,   `ordering` smallint(6) not null,   `ready_to_begin` tinyint(1) not null,   `in_progress` tinyint(1) not null,   `is_conformed` tinyint(1) not null,   `order_item_equivalent` varchar(60) default '',   primary key (`id`) ) engine=innodb auto_increment=122 default charset=latin1; 

i need insert record in mysql database if not exists, below query trying use.

    insert ignore `tasks_taskstatus`  set `name`='saveout - in progress',  `video_status`=1,  `creative_status`=0,  `missing_asset`=0,  `is_finished`=0,  `share_with_am`=0,  `ordering`=4,  `ready_to_begin`=0,  `in_progress`=1,  `is_conformed`=0,  `order_item_equivalent`=''; 

but everytime run query new record being created, what's wrong in above statement , how can modify above query in order insert data/record if not exists?

you don't insert id (which primary key), auto_increment kicks in , generates new id , inserts new record.

ask yourself: how mysql know which record update? if know (an example answer "by name"), can change keys , should work.

if combination of name , video_status has unique, create key:

alter table `tasks_taskstatus` add unique(`name`, `video_status`); 

Comments