mysql conditional insert -


can have sql insert this?

insert a_has_b (a_id,b_id) values (1,2) if not (select count(a_id) a_has_b a_id = 1 , b_id = 2);

to answer question, do:

insert a_has_b ( a_id, b_id ) select 1, 2   a_has_b  a_id = 1 , b_id = 2; 

however, think looking insert ignore:

insert ignore a_has_b ( a_id, b_id ) values (1, 2); 

this way ignore if there duplicate rows , have proper primary key on values.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -